Code Monkey home page Code Monkey logo

Comments (7)

diegohaz avatar diegohaz commented on May 20, 2024

Could you elaborate more?

from constate.

lishine avatar lishine commented on May 20, 2024

container.setState({count: 1})
Not in react component

from constate.

lishine avatar lishine commented on May 20, 2024

Like easy-peasy for example

from constate.

diegohaz avatar diegohaz commented on May 20, 2024

Got it. No, it doesn't work.
If people come up with use cases and API/implementation ideas, we can think about something like this.

from constate.

lishine avatar lishine commented on May 20, 2024

In Next.js
I want to make the pathname be available to all components.
I assign the pathname to global store in getInitialProps.
But getInitialProps is get called before render.
Same for authentication status

from constate.

diegohaz avatar diegohaz commented on May 20, 2024

Does something like this work?

// normal context
PathContext = React.createContext(null);

// in getInitialProps
return { path };

// in app render
<PathContext.Provider value={this.props.path}>
  ...
</PathContext.Provider>

// in another child component
const path = React.useContext(PathContext);
console.log(path);

from constate.

lishine avatar lishine commented on May 20, 2024

Yes, thanks for the illustration.
This pathname is assigned in the _app getInitialProps. But it is not available in other components' getInitialProps. Of course in this case, the other components' getInitialProps receives pathname anyway, but for authentication status I can add it to it's props. So it is solvalbe of course.
But suppose I have some other store which is not in a component but outside - it cannot access the context, so it cannot get these variables.

from constate.

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.