GithubHelp home page GithubHelp logo

Comments (6)

Ykk2 avatar Ykk2 commented on April 17, 2024

I believe this is happening because when you store an entire component inside the state, it is stored with the snapshot of the props at that moment.

Also, in general I think it is discouraged to store components inside the state. Even if the component will not be updated. From personal experience I found this leads to unexpected behavior with bugs that are hard to track down. Instead now I set the state with a unique identifier (str, or int), which will then render the component I want based on the value of the state.

Could you share with me what your intentions were with this approach? I think it can help me understand the situation better.

from react.

imageck avatar imageck commented on April 17, 2024

I believe this is happening because when you store an entire component inside the state, it is stored with the snapshot of the props at that moment.

Right, I suspected the same.

Could you share with me what your intentions were with this approach? I think it can help me understand the situation better.

The idea is to limit the number of times any particular option can be selected. That is, an option must not be displayed (and consequently selected) if it's already been selected, say, three times. If the props were live, the last part of the code (from line 90) would be executed as intended.

from react.

Ykk2 avatar Ykk2 commented on April 17, 2024

Gotcha, would this solution work for you?

https://stackblitz.com/edit/nextjs-hxcvxr?file=pages%2Findex.js

Instead of storing the component, we are just storing objects that will be passed down as props.
Then we create a local state within each ItemField component and listen in on types to update the component.

from react.

imageck avatar imageck commented on April 17, 2024

Impressive!

I think I have taken a similar approach the second or the third version but it led to duplicate entries and a broken state. Possibly due to updating types improperly.

One thing. I'm not quite sure why there is a need for a separate state variable in options, as I believe prop updates already cause a re-render. Could you please shed some light on why duplication of state is okay here? The comment is a bit confusing, at least to me. Removing this new local state doesn't seem to affect the component in any negative way.

Anyway, you've just put an end to a lot of my frustrations. Thank you for taking the time to help me overcome this roadblock!

P.S. I'll apply your changes to the real component tomorrow.

from react.

Ykk2 avatar Ykk2 commented on April 17, 2024

Glad it's working! I chose to duplicate state here and keep it updated to ensure each component has an accurate reference to the current state of types. However, I do agree that this could be an unnecessary precaution.

from react.

gaearon avatar gaearon commented on April 17, 2024

You're really not supposed to be storing JSX in state. Store the data itself. Then update the data when needed.

from react.

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.