Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDavid Axelrod
36,073 PointsStill confused as to what the purpose of actions are
I understand they represent actions uses can take but why do they exist if you can write the control flow in the components
2 Answers
Abraham Juliot
47,353 PointsSince Actions send data from the app to the Redux store, a deeper question is why use Redux when we can manage data in the component? There's an article on this that sheds some light on the subject: "Redux is used mostly for application state management. To summarize it, Redux maintains the state of an entire application in a single immutable state tree (object), which can't be changed directly. When something changes, a new object is created (using actions and reducers)." - https://www.smashingmagazine.com/2016/06/an-introduction-to-redux/
Also, check out the article When to use Redux by Ian Carlson: "Redux actions and reducers can create some extra boiler plate so itβs good to have a standard set of criteria on when to use it..." - https://medium.com/@fastphrase/when-to-use-redux-f0aa70b5b1e2
Alexander La Bianca
15,959 PointsAbraham posted a good answer. To add to that I can say that keeping your state organized in a single state tree helps a lot with keeping a single source of truth. Imagine you have dozens of components that all need to render the same data and also react to changes of that same data. Keeping that local to each component will be difficult to maintain and track down bugs. If you kept it is a redux store you simply subscribe to the store and know your data only lives in there and as many components as you want can render that data without much complexity.
David Axelrod
36,073 PointsDoes having an application wide store also make it easier to change state from child components? Like instead of having chained callback functions to change the application state, you can just access the application store from anywhere?
Alexander La Bianca
15,959 PointsYes. As long you are using actions to trigger the state changes you are free to trigger them in any component. I am not too familiar with best practices in react, but I adopted the redux pattern in my Angular 4 application. Even though I could trigger actions from any child component i decide to not do so and bubble events up to parent smart component that then triggers the actions to the store. It just sort of helps me to keep track to which component ultimately does change state and which one's do not
David Axelrod
36,073 PointsAwesome! Thanks Alex
David Axelrod
36,073 PointsDavid Axelrod
36,073 PointsWow thanks, the articles really helped!