GithubHelp home page GithubHelp logo

Comments (4)

atulmy avatar atulmy commented on May 7, 2024 1

I was wondering same. My use case was more around the user login state from localStorage. I ended up doing this:

Redux (inside /src/index.js):

// User Authentication
const token = window.localStorage.getItem('token')
if (token && token !== 'undefined' && token !== '') {
  const user = JSON.parse(window.localStorage.getItem('user'))
  if (user) {
    loginSetUserLocalStorage(token, user)
    store.dispatch(loginSetUser(token, user))
  }
}

Recoil (inside /src/user/api/state.js):

const userAuthDefault = {
  isAuthenticated: false,
  user: null
}

// on page load
// try to get user token and details from localStorage
const token = window.localStorage.getItem('token')
if (token && token !== 'undefined' && token !== '') {
  const user = JSON.parse(window.localStorage.getItem('user'))

  if (user) {
    userAuthDefault.isAuthenticated = true
    userAuthDefault.user = user
  }
}

// auth
export const userAuth = atom({
  key: 'userAuth',
  default: userAuthDefault
})

Hope this will help for some use cases.

from recoil.

davidmccabe avatar davidmccabe commented on May 7, 2024

It's not currently possible. Recoil state is not global but exists within a particular React context, and there is also no "current" state, just the most recently committed state from the point of view of a particular component.

If you tell me what you actually need to do then I can suggest how to do it.

from recoil.

JesperLekland avatar JesperLekland commented on May 7, 2024

We have a pretty big brownfield app, meaning that we would need to gradually migrate from our current solution of Redux (which have the feature in question, and which we use to a certain extent). We also have a few "external" modules that are agnostic to the component context but still have access to the state, e.g an API module, a deep-linking module etc.

from recoil.

keene-dalveytech avatar keene-dalveytech commented on May 7, 2024

It looks like an ugly way to write it

from recoil.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.