Search code examples
javascriptreactjsflux

Do I need Flux Store for this purpose?


Let's say I have a simple form component for posting a tweet. Why would I want to create a store to keep my form state, instead of just declaring the state in getInitialState() of my form component ? After the user has typed in the form and presses submit, then only we update the state and call backend api? Why would I go through trouble of setting up action and listeners? Thanks for looking at this!


Solution

  • Short answer is no. A simple react component can be completely self sufficient.

    A Long answer is that it depends if this component coexists within a larger application.

    If a posted tweet forces some other component to be updated then you need some form of communication to handle that. This can be simple function or callback for a small number of components and dependencies. But as an app grows, the cascade of dependencies will grow with it, and reasoning about multiple components becomes harder. The "flux" pattern is a proven way to tackle this problem.

    My advice: Start small and keep state and components simple, refactor as necessary.