The course is part of this learning path
This module looks at how to add and update state to a variety of different components.
The objectives of this module are to provide you with an understanding of:
- How to add and update state to class and function components
This learning path is aimed at all who wish to learn how to use the ReactJS framework.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
- State is often dated in an application due to some event. Without user interaction or an external influence. A really important concept to mention right from the off here, is that you should never directly mutate the value of state. If it needs to be updated, this should be done using the updater of function defined when state was declared. Why? Calling the updater to function triggers a re-render of the component that owns the state. The different algorithm will detect that the value of state has changed and then cascade this change down to every component that uses the state value. The only component will obviously be re-render, but any component that receives the value as part of its props will to mutating the value directly bypasses this process. I should not be done for this reason. We'll cover events in a lot more detail when we're looking to invest data flow, but for now it's sufficient for us to know that we can use any of the events from the API as long as we camel case them. This is for JSX interpretation to distinguish them from HTML attributes. Most will work without camel casing, but there'll be a friendly warning on the Console if you do this. You can see in the code for the component with state, we have added a button on line 11 that has an Onclick event. We supplied the Onclick value as a JSX expression. In this case, we define a callback hourly function that calls set camp with an argument of count plus one clicking on the button in the browser. You'll say expected result. The value displayed increases by one. If we inspect the React Developer tools, we can see that the value in the hooks State increases when the button is clicked. We should also note, that the countUser component updates it's display as the value supplied to it, is props. Either count state changes when ComponentWithState is re-rendered using the new value of count.
About the Author