|
| 1 | +# 05 Refactor |
| 2 | + |
| 3 | +In the previous sample we were setting an initial username value, what would |
| 4 | +happen if we expect this value to come from e.g. an AJAX request or if it could |
| 5 | +change in time? The current approach won't work. |
| 6 | + |
| 7 | +We can think about two possible solutions: |
| 8 | + |
| 9 | +- The first idea that could come into our mind is to implement a mix: we receive via props the current name value, then we hold an state with the current editing |
| 10 | +value... what drawbacks could we encounter? We have to listen on the `componentWillReceiveProps` for any change on the parent user name control and replace our state, we end up with a mixed governance. |
| 11 | + |
| 12 | +- The second idea is to setup two properties, the parent control will hold _userName_ and _editingUsername_, whenever the user clicks on the button to |
| 13 | +replace the name it will notify the parent control and it will replace the |
| 14 | +content of _userName_" with the content from _editingUsername_. If _userName_ gets updated by any other third party (e.g. ajax callback) it will update as well |
| 15 | +_editingUsername_. |
| 16 | + |
| 17 | +We will take as a starting point sample _04 Callback_: |
| 18 | + |
| 19 | +Summary steps: |
| 20 | + |
| 21 | +- Update _nameEdit.jsx_ in order to request the new _editingUsername_, and remove it from the state. |
| 22 | + |
| 23 | +- Update _app.jsx_ to hold the new editing property in the state, pass it to the |
| 24 | +children, control and perform the proper update on the callback event from the |
| 25 | +child control. |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +Install [Node.js and npm](https://nodejs.org/en/) if they are not already installed on your computer. |
| 30 | + |
| 31 | +> 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. |
| 32 | +
|
| 33 | +## Steps to build it |
| 34 | + |
| 35 | +- Copy the content from _04 Callback_ and execute `npm install`. |
| 36 | + |
| 37 | +- Update _nameEdit.jsx_ in order to request the new _editingUsername_, and remove it |
| 38 | +from the state. |
| 39 | + |
| 40 | +```jsx |
| 41 | +import React from 'react'; |
| 42 | + |
| 43 | +export class NameEditComponent extends React.Component { |
| 44 | + constructor(props) { |
| 45 | + super(props); |
| 46 | + } |
| 47 | + |
| 48 | + render() { |
| 49 | + return ( |
| 50 | + <div> |
| 51 | + <label>Update Name:</label> |
| 52 | + <input value={this.props.editingUserName} |
| 53 | + onChange={(e) => this.props.onEditingNameUpdated(e.target.value)} /> |
| 54 | + <input type="submit" value="Change" className="btn btn-default" |
| 55 | + onClick={this.props.onNameUpdateRequest} /> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +NameEditComponent.propTypes = { |
| 62 | + editingUserName: React.PropTypes.string.isRequired, |
| 63 | + onEditingNameUpdated: React.PropTypes.func.isRequired, |
| 64 | + onNameUpdateRequest: React.PropTypes.func.isRequired |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +- Update _app.jsx_ to hold the new editing property in the state, pass it to the |
| 69 | +children control and perform the proper update on the callback event from the |
| 70 | +child control. |
| 71 | + |
| 72 | + |
| 73 | +```jsx |
| 74 | +import React from 'react'; |
| 75 | +import {HelloComponent} from './hello'; |
| 76 | +import {NameEditComponent} from './nameEdit'; |
| 77 | + |
| 78 | +export class App extends React.Component { |
| 79 | + constructor(props) { |
| 80 | + super(props); |
| 81 | + |
| 82 | + const defaultUserName = 'defaultUserName'; |
| 83 | + this.state = {userName: defaultUserName, editingUserName: defaultUserName}; |
| 84 | + } |
| 85 | + |
| 86 | + setUsernameState() { |
| 87 | + this.setState({userName: this.state.editingUserName}); |
| 88 | + } |
| 89 | + |
| 90 | + updateEditingName(editingName) { |
| 91 | + this.setState({editingUserName: editingName}); |
| 92 | + } |
| 93 | + |
| 94 | + render() { |
| 95 | + return ( |
| 96 | + <div> |
| 97 | + <HelloComponent userName={this.state.userName} /> |
| 98 | + <NameEditComponent |
| 99 | + editingUserName={this.state.editingUserName} |
| 100 | + onEditingNameUpdated={this.updateEditingName.bind(this)} |
| 101 | + onNameUpdateRequest={this.setUsernameState.bind(this)} /> |
| 102 | + </div> |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
| 106 | + ``` |
| 107 | + |
| 108 | +Finally we can check the sample is working as _04 Callback_ executing from the command line |
| 109 | +`npm start` and opening [http://localhost:8080](http://localhost:8080). |
0 commit comments