Skip to content

Preserving state through Props-turtleshell #6616

@evenstensberg

Description

@evenstensberg

Preserving state through Props-turtleshell

Been experimenting a bit with migrating props and state together and watch changes and how they could be used to preserve a state through multiple render passes. As addressed in #4595 , I'd like to see how this approach would turn out to be.

Props are much better to pass values than state, and how people will use this feature would be exciting to hopefully see!

To first point out some flaws that needs fix in order for this to fully function (from my perspective)

  1. A very own special method that only keeps track of state that has been called upon and not the entire tree.
  2. Loose state wrappers

I'll go in-depth later, after explaining how this would look

Fetching state and passing them as props


For now, I've just used a deep-expensive method. For stylus-sake, I've used a function as a wrapper to make it more approachable, like you can see in the picture.


skjermbilde 2016-04-25 kl 22 25 54


Now, there's a lot of sketchy things going on in the onClick attribute, but this is highly experimental for now. What is really good about this solution, is that it doesn't nest states as children, as other proposals. It rather passes your state as a prop and then updates the prop based on the previous state. ( I honestly don't have a clue why I added transferProps with this code )


And that is the intended use case here. To avoid having state as children of the parent state or as children methods. This code is also highly reusable, once perfected. For now, this is not-so-highly performance-friendly to reuse, mainly because I'm using replaceState which in itself is a expensive task. Also, it breaks, when used in the long term, like this:


giphy

skjermbilde 2016-04-25 kl 19 07 10

State to Props lifecycle functions

As mentioned, I used replaceState. Could also have used setState, but really, we should not have any of those for this feature. If this feature is a good way to keep track of state, I'd like to see the following:

  • State-to-props lifecycle

    Why? setState and replaceState should only be set on the existing state. The new prop returned would allow us to take the previous state, and ignore any other value passed by previous states. This would allow us a much faster transition from prevState to theNewProp, where we just update state as a new value of prop, rather than re-rendering the entire state tree.

  • Props-to-state lifecycle

    Why? We need some insurance that state is a valid state. Maybe as a callback function that takes the current props and allows you to set a new state value, like getInitialState with props as its arguments.

We've covered the most here. Maybe one thing to add up, is that we don't need to keep track of the entire state tree, as long as we get the previous returned state valid. This brings me up to issue 2. I'm not sure how React is set up with this right now with state wrappers, but I think it will need some fixes if this is implemented/considered.

(I tried making this as an addon without breaking encapsulation. It didn't work because I was trying to make the lifecycle functions/methods from external modules.( Yeah, I'm a terrible programming citizen 😭 ))

To clarify: For this, we need 2 new Lifecycle functions

Making sure people gets informed about this issue, @sebmarkbage @gaearon @zpao @jimfb

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions