3 回答
TA贡献1821条经验 获得超6个赞
setState
asynchronous
setState
this.state
....changeTitle: function changeTitle (event) { this.setState({ title: event.target.value }); this.validateTitle();},validateTitle: function validateTitle () { if (this.state.title.length === 0) { this.setState({ titleError: "Title can't be blank" }); }},....
title
render()
....changeTitle: function changeTitle (event) { this.setState({ title: event.target.value }, function() { this.validateTitle(); });},validateTitle: function validateTitle () { if (this.state.title.length === 0) { this.setState({ titleError: "Title can't be blank" }); }},....
dispatch
render()
API Call
onState
Child Component
setState callback
....changeTitle: function (event) { this.setState({ title: event.target.value }, () => this.APICallFunction());},APICallFunction: function () { // Call API with the updated value}....
TA贡献1820条经验 获得超2个赞
api
each
changeSearchParams = (params) => { this.setState({ params }, this.performSearch)} performSearch = () => { API.search(this.state.params, (result) => { this.setState({ result }) });}
因此,对于任何状态更改,都可以在呈现方法主体中执行操作。
非常糟糕的做法render
componentDidMount
添加回答
举报