Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The official TodoMVC example is still using imperative state updates? #2407

Closed
dmitriz opened this issue May 16, 2017 · 2 comments
Closed

The official TodoMVC example is still using imperative state updates? #2407

dmitriz opened this issue May 16, 2017 · 2 comments

Comments

@dmitriz
Copy link

dmitriz commented May 16, 2017

This line with imperative state updates makes me puzzled to see it in the Redux architecture

https://github.com/reactjs/redux/blob/master/examples/todomvc/src/components/TodoTextInput.js#L22

      if (this.props.newTodo) {
        this.setState({ text: '' })
      }

This seems to be exactly the style making functions impure and harder to test, that Redux is aimed to discourage or better ban altogether.

Was it an oversight to leave it like that?

@gaearon
Copy link
Contributor

gaearon commented May 16, 2017

It's intentional. In many cases it's easier to use React state for small isolated updates.
http://redux.js.org/docs/faq/OrganizingState.html#organizing-state-only-redux-state

@gaearon gaearon closed this as completed May 16, 2017
@dmitriz
Copy link
Author

dmitriz commented May 16, 2017

@gaearon

Thank you very much for quickly commenting on that
and I can see where your reasoning come from,
but isn't the real reason why it is easier
is due to the enforced global state model?

If the local state management were supported,
you could just write a small reducer function next to the component
with its own private action,
making your component completely pure and testable?

The problem I see here is not so much for this particular example,
where things are indeed simple enough this way because the component itself is simple,
but once you try to imitate it in the more complex advanced cases,
where the imperative style will increasingly begin to "bite",
pushing you into more complex but still imperative-style-based architecture,
until and at some stage, you would have to throw away much of it
and move state to Redux, which could be considerably harder at that later stage.

It feels like a fundamental problem of co-existing of two different architecture patterns,
competing with each other to solve the same problem,
which inevitably creates additional complexity,
and the painful question which way you implement this and which way that,
will "haunt" every piece of the app :)

Whereas, if local state was allowed by Redux, it could have been easy the other way around --
have pure functional uniform structure instead of the imperative one mixed in here and there.

Here is an example of how it may look:
https://github.com/dmitriz/un/blob/master/examples/basic/submit-with-reset/index.js#L5
It uses streams but the architecture is very close to redux,
with the additional flexibility of local reducers.

I can see there is a long discussion with many people expressing their reasons
to depart from the monolith single state model that is even linked from the page in your link :),
but no complete answer to justify the current situation seems provided unless I missed it: #1385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants