|
| 1 | +# 03 State |
| 2 | + |
| 3 | +In this example we introduce a basic React concept: handling State. |
| 4 | + |
| 5 | +In this scenario we provide a default username and let the user update it. |
| 6 | + |
| 7 | +We take as a starting point the example _02 Properties_: |
| 8 | + |
| 9 | +## Summary steps: |
| 10 | + |
| 11 | +- Create an _App_ component that holds the state. This state will contain the current |
| 12 | + username (with default value "defaultUserName"). |
| 13 | + This _App_ component renders the _Hello_ component. At first we create a simple stateless |
| 14 | + _App_ component. |
| 15 | +- Update _main.tsx_ file to include our _App_ component. |
| 16 | +- Change _App_ component to a stateful class component to hold the _userName_ state. |
| 17 | +- Create a _NameEdit_ component to let the user change the value of username. This changes the _App_ state |
| 18 | + using a function from _App_. |
| 19 | +- Check everything works properly. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +Install [Node.js and npm](https://nodejs.org) if they are not already installed on your computer. |
| 24 | + |
| 25 | +> Verify that you are running at least node v6.x.x and npm 3.x.x by running `node -v` and `npm -v` in a terminal/console window. Older versions may produce errors. |
| 26 | +
|
| 27 | +## Steps to build it |
| 28 | + |
| 29 | +- Copy the content from _02 Properties_ and execute `npm install`. |
| 30 | + |
| 31 | +- Let's create an _App_ component under a new file named _app.tsx_ (this component will display the _Hello_ component). |
| 32 | + |
| 33 | +_./src/app.tsx_ |
| 34 | + |
| 35 | +```jsx |
| 36 | +import * as React from "react"; |
| 37 | +import { HelloComponent } from "./hello"; |
| 38 | + |
| 39 | +export const App = () => { |
| 40 | + return <HelloComponent userName="John" />; |
| 41 | +}; |
| 42 | +``` |
| 43 | + |
| 44 | +- Let's update _index.tsx_ just to use the _App_ component that we have just created. |
| 45 | + |
| 46 | +_./src/index.tsx_ |
| 47 | + |
| 48 | +```diff |
| 49 | + import * as React from 'react'; |
| 50 | + import * as ReactDOM from 'react-dom'; |
| 51 | ++ import { App } from './app'; |
| 52 | + |
| 53 | +- import { HelloComponent } from './hello'; |
| 54 | + |
| 55 | + ReactDOM.render( |
| 56 | +- <HelloComponent userName="John" />, |
| 57 | ++ <App />, |
| 58 | + document.getElementById('root') |
| 59 | + ); |
| 60 | +``` |
| 61 | + |
| 62 | +- Now we can check that things are still working as expected. |
| 63 | + |
| 64 | + ``` |
| 65 | + npm start |
| 66 | + ``` |
| 67 | + |
| 68 | +- It's time to revisit _app.tsx_. We want to store the user's name and let the user updated it. We will use hooks to |
| 69 | + allow _App_ fucntional components to make use of state (this works in React 16.8.2 and above if you have to use |
| 70 | + older verions you have to use a class component, check the "old*classes_components" on the root of this repo for example). |
| 71 | + We will add \_userName* to the state. |
| 72 | + |
| 73 | +Let's move this component to a class stateful component and define a state including _userName_, and pass this value to the _Hello_ component. |
| 74 | + |
| 75 | +_./src/app.tsx_ |
| 76 | + |
| 77 | +```diff |
| 78 | +import * as React from "react"; |
| 79 | + |
| 80 | +import { HelloComponent } from "./hello"; |
| 81 | + |
| 82 | +export const App = () => { |
| 83 | ++ const [name, setName] = React.useState('defaultUserName'); |
| 84 | +- return <HelloComponent userName="John" />; |
| 85 | ++ return <HelloComponent userName={name} />; |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +- Again, we can do a quick check to test that everything works as expected. |
| 90 | + |
| 91 | +``` |
| 92 | +npm start |
| 93 | +``` |
| 94 | + |
| 95 | +- Now it's time to create an _NameEdit_ component. This component lets the user update his username and notifies with a callback to the parent control whenever the value of _userName_ gets updated. |
| 96 | + |
| 97 | +_./src/nameEdit.tsx_ |
| 98 | + |
| 99 | +```jsx |
| 100 | +import * as React from "react"; |
| 101 | + |
| 102 | +interface Props { |
| 103 | + userName: string; |
| 104 | + onChange: (e: React.ChangeEvent<HTMLInputElement>) => void; |
| 105 | +} |
| 106 | + |
| 107 | +export const NameEditComponent = (props: Props) => ( |
| 108 | + <> |
| 109 | + <label>Update name:</label> |
| 110 | + <input value={props.userName} onChange={props.onChange} /> |
| 111 | + </> |
| 112 | +); |
| 113 | +``` |
| 114 | + |
| 115 | +Side note: What is this Fragment or <> stuff? A way to create component that has multiple root elements (not a single parent). Available from React 16.2. As an alternative you can type: |
| 116 | + |
| 117 | +```jsx |
| 118 | + ... |
| 119 | + export const NameEditComponent = (props : Props) => |
| 120 | + <React.Fragment> |
| 121 | + <label>Update name:</label> |
| 122 | + <input value={props.userName} |
| 123 | + onChange={props.onChange} |
| 124 | + /> |
| 125 | + </React.Fragment> |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +- In the _app.tsx_ file, let's add a function to replace the state value of _userName_ with the new one. |
| 130 | + |
| 131 | +_./src/app.tsx_ |
| 132 | + |
| 133 | +```diff |
| 134 | +import * as React from "react"; |
| 135 | +import { HelloComponent } from "./hello"; |
| 136 | +import { NameEditComponent } from './nameEdit'; |
| 137 | +import { NameEditComponent } from './nameEdit'; |
| 138 | + |
| 139 | + |
| 140 | +export const App = () => { |
| 141 | + const [name, setName] = React.useState("defaultUserName"); |
| 142 | + |
| 143 | ++ const setUsernameState = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 144 | ++ setName(event.target.value); |
| 145 | ++ } |
| 146 | + |
| 147 | +- return <HelloComponent userName={name} />; |
| 148 | ++ return ( |
| 149 | ++ <> |
| 150 | ++ <HelloComponent userName={name} /> |
| 151 | ++ <NameEditComponent userName={name} onChange={setUsernameState} /> |
| 152 | ++ </> |
| 153 | ++ ); |
| 154 | +}; |
| 155 | +``` |
| 156 | + |
| 157 | +Side note: mind the use of the fat arrow function. This avoids losing the context for _this_ in the callback. |
| 158 | + |
| 159 | +- Finally let's test everything works once more. |
| 160 | + |
| 161 | + ``` |
| 162 | + npm start |
| 163 | + ``` |
0 commit comments