|
| 1 | +# 04 Callback + State |
| 2 | + |
| 3 | +In this sample we are going to refactor the previous sample **03 State**. |
| 4 | + |
| 5 | +We'll update the name property only when the user clicks on |
| 6 | +a _change_ button, we will simplify the event itself as well. |
| 7 | + |
| 8 | +Obviously, we will take the sample **03 State** as a starting point. |
| 9 | + |
| 10 | +Summary steps: |
| 11 | + |
| 12 | +- Add a button to the `EditName` component and a handler function for this. |
| 13 | +- Submit the name only when the user clicks on the button. |
| 14 | +- Update the `app` component to handle the new simplified event. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +Install [Node.js and npm](https://nodejs.org/en/) (v6.6.0 or newer) if they are not already installed on your computer. |
| 19 | + |
| 20 | +> 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. |
| 21 | +
|
| 22 | +## Steps to build it |
| 23 | + |
| 24 | +- Copy the content of the `03 State` folder to an empty folder for the sample |
| 25 | +and make this your current folder. |
| 26 | + |
| 27 | +- Install the npm packages described in the `package.json` and verify that it works: |
| 28 | + |
| 29 | + ```bash |
| 30 | + npm install |
| 31 | + ``` |
| 32 | + |
| 33 | +- Since we are going to use an internal handler, we'll transform the `NameEditComponent` |
| 34 | +from a stateless component into a class component, then we will add some refactor on the naming. |
| 35 | + |
| 36 | + The `nameEdit.jsx` file should looks like this: |
| 37 | + |
| 38 | + ```jsx |
| 39 | + import * as React from 'react'; |
| 40 | + |
| 41 | + export class NameEditComponent extends React.Component { |
| 42 | + |
| 43 | + constructor(props) { |
| 44 | + super(props); |
| 45 | + // Watch out what would happen if we get this user name via an AJAX callback |
| 46 | + // you will find a different implementatin on 05 sample |
| 47 | + this.state = { |
| 48 | + editingName: this.props.initialUserName, |
| 49 | + }; |
| 50 | + |
| 51 | + this.onChange = this.onChange.bind(this); |
| 52 | + this.onNameSubmit = this.onNameSubmit.bind(this); |
| 53 | + } |
| 54 | + |
| 55 | + onChange(event) { |
| 56 | + this.setState({ editingName: event.target.value }); |
| 57 | + } |
| 58 | + |
| 59 | + onNameSubmit() { |
| 60 | + this.props.onNameUpdated(this.state.editingName); |
| 61 | + } |
| 62 | + |
| 63 | + render() { |
| 64 | + return ( |
| 65 | + <div> |
| 66 | + <label htmlFor="editingName">Update Name:</label> |
| 67 | + <input value={this.state.editingName} onChange={this.onChange} id="editingName" /> |
| 68 | + <input type="submit" value="Change" className="btn btn-default" onClick={this.onNameSubmit} /> |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + NameEditComponent.propTypes = { |
| 75 | + initialUserName: React.PropTypes.string.isRequired, |
| 76 | + onNameUpdated: React.PropTypes.func, |
| 77 | + }; |
| 78 | + ``` |
| 79 | + |
| 80 | +- Let's wire this up in the `app.jsx` file. |
| 81 | + |
| 82 | + ```jsx |
| 83 | + export class App extends React.Component { |
| 84 | + constructor(props) { |
| 85 | + super(props); |
| 86 | + this.state = { userName: 'defaultUserName' }; |
| 87 | + this.setUsernameState = this.setUsernameState.bind(this); |
| 88 | + } |
| 89 | + |
| 90 | + setUsernameState(newName) { |
| 91 | + this.setState({ userName: newName }); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <HelloComponent userName={this.state.userName} /> |
| 98 | + <NameEditComponent |
| 99 | + initialUserName="Javier Cansado" |
| 100 | + onNameUpdated={this.setUsernameState} |
| 101 | + /> |
| 102 | + </div> |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + ``` |
| 108 | + |
| 109 | + Now we've got a clear event, strongly typed and simplified (straight forward). |
| 110 | + |
| 111 | +- Let's give it a try: |
| 112 | + |
| 113 | + ```bash |
| 114 | + npm start |
| 115 | + ``` |
| 116 | + |
| 117 | +- Then, load http://localhost:8080/ in a browser to see the output. |
| 118 | + |
| 119 | +  |
| 120 | + |
| 121 | + Now, the greeting only change when the user clicks on the change button. |
0 commit comments