Browse Source

Move handleChange above render

main
Dan Abramov 7 years ago
committed by GitHub
parent
commit
ff9dc0bab2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 40
      content/blog/2018-06-07-when-to-use-derived-state.md

40
content/blog/2018-06-07-when-to-use-derived-state.md

@ -57,13 +57,13 @@ class EmailInput extends Component {
return null; return null;
} }
render() {
return <input onChange={this.handleChange} value={this.state.email} />;
}
handleChange = event => { handleChange = event => {
this.setState({ email: event.target.value }); this.setState({ email: event.target.value });
}; };
render() {
return <input onChange={this.handleChange} value={this.state.email} />;
}
} }
``` ```
@ -140,13 +140,13 @@ class EmailInput extends Component {
email: this.props.defaultEmail email: this.props.defaultEmail
}; };
render() {
return <input onChange={this.handleChange} value={this.state.email} />;
}
handleChange = event => { handleChange = event => {
this.setState({ email: event.target.value }); this.setState({ email: event.target.value });
}; };
render() {
return <input onChange={this.handleChange} value={this.state.email} />;
}
} }
``` ```
@ -291,6 +291,10 @@ class Example extends Component {
return null; return null;
} }
handleChange = event => {
this.setState({ filterText: event.target.value });
};
render() { render() {
return ( return (
<Fragment> <Fragment>
@ -299,10 +303,6 @@ class Example extends Component {
</Fragment> </Fragment>
); );
} }
handleChange = event => {
this.setState({ filterText: event.target.value });
};
} }
``` ```
@ -317,6 +317,10 @@ class Example extends PureComponent {
filterText: "" filterText: ""
}; };
handleChange = event => {
this.setState({ filterText: event.target.value });
};
render() { render() {
const filteredList = this.props.list.filter( const filteredList = this.props.list.filter(
item => item.text.includes(this.state.filterText) item => item.text.includes(this.state.filterText)
@ -329,10 +333,6 @@ class Example extends PureComponent {
</Fragment> </Fragment>
); );
} }
handleChange = event => {
this.setState({ filterText: event.target.value });
};
} }
``` ```
@ -354,6 +354,10 @@ class Example extends Component {
) )
); );
handleChange = event => {
this.setState({ filterText: event.target.value });
};
render() { render() {
const filteredList = this.filter(this.props.list, this.state.filterText); const filteredList = this.filter(this.props.list, this.state.filterText);
@ -364,10 +368,6 @@ class Example extends Component {
</Fragment> </Fragment>
); );
} }
handleChange = event => {
this.setState({ filterText: event.target.value });
};
} }
``` ```

Loading…
Cancel
Save