应该如何使用 ReactJS 中的 setState() 方法?
How should one use the setState() method in ReactJS?
我是 ReactJS 的新手,我正在尝试理解 state
和 setState()
。使用 setState()
我想更改名称,但我不确定应该在代码中的何处调用 setState()
方法:
- 构造函数内部 OR
- 渲染方法内部或
- 创建一个自定义方法并在
render()
调用之前在构造函数的末尾调用它
这是我的代码:
import React from "react";
class StateBasic extends React.Component{
constructor(){
super();
let personProfile = this.state = {
name : "Bob",
skill : "Art Designer",
location : "LA"
}
console.log(personProfile);
}
render(){
let changeName = this.setState({ name : "Frank" });
return(
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
<li> {changeName} </li>
</ul>
</div>
);
}
}
// Let's render ReactDOM
export default StateBasic;
setState
通常发生(但不限于)当用户和您的应用程序之间存在某种交互时。例如:
- 当用户输入内容时
- 当用户单击按钮时
class StateExample extends React.Component {
constructor() {
super()
this.state = {
clickTimes: 0,
value: '',
}
this.handleChange = this.handleChange.bind(this)
this.handleClick = this.handleClick.bind(this)
}
handleChange(event) {
this.setState({ value: event.target.value })
}
handleClick() {
this.setState({ clickTimes: this.state.clickTimes + 1 })
}
render() {
return (
<div>
<label>Type here:</label>
<input type="text" value={this.state.value} onChange={this.handleChange} />
<div style={{ marginTop: 20 }}>
<button onClick={this.handleClick}>Click-me</button>
Click times: {this.state.clickTimes}
</div>
</div>
)
}
}
ReactDOM.render(
<StateExample />,
document.getElementById('root')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
有关更多信息,我建议阅读 ReactJS 文档中的 State and Lifecycle。
如果你初始化状态然后在构造函数中做
constructor(){
super();
let personProfile = this.state = {
name : "Bob",
skill : "Art Designer",
location : "LA"
}
console.log(personProfile);
this.state= { name : "Frank" };//initialvalue
}
如果你想改变一些动作,那么制作一个方法(changeName
)并在渲染中像这样使用:
changeName(name){
this.setState({name})
}
render(){
let changeName = this.setState({ name : "Frank" });
return(
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
<li onClick={this.changeName.bind(this,"hello")} > change Me </li>
</ul>
</div>
);
}
如果您在 render()
方法中调用 setState
,您将创建无限循环,您可以使用 componentDidMount
.
class StateBasic extends React.Component {
constructor() {
super();
let personProfile = this.state = {
name: "Bob",
skill: "Art Designer",
location: "LA"
}
}
componentDidMount() {
setTimeout(() => {
this.setState({name: "Frank"});
}, 1000)
}
render() {
return (
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
</ul>
</div>
);
}
}
ReactDOM.render( <
StateBasic / > ,
document.getElementById('container')
)
<script src="https://unpkg.com/react@16.0.0/umd/react.development.js"></script>
<div id="container"></div>
<script src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.development.js"></script>
我是 ReactJS 的新手,我正在尝试理解 state
和 setState()
。使用 setState()
我想更改名称,但我不确定应该在代码中的何处调用 setState()
方法:
- 构造函数内部 OR
- 渲染方法内部或
- 创建一个自定义方法并在
render()
调用之前在构造函数的末尾调用它
这是我的代码:
import React from "react";
class StateBasic extends React.Component{
constructor(){
super();
let personProfile = this.state = {
name : "Bob",
skill : "Art Designer",
location : "LA"
}
console.log(personProfile);
}
render(){
let changeName = this.setState({ name : "Frank" });
return(
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
<li> {changeName} </li>
</ul>
</div>
);
}
}
// Let's render ReactDOM
export default StateBasic;
setState
通常发生(但不限于)当用户和您的应用程序之间存在某种交互时。例如:
- 当用户输入内容时
- 当用户单击按钮时
class StateExample extends React.Component {
constructor() {
super()
this.state = {
clickTimes: 0,
value: '',
}
this.handleChange = this.handleChange.bind(this)
this.handleClick = this.handleClick.bind(this)
}
handleChange(event) {
this.setState({ value: event.target.value })
}
handleClick() {
this.setState({ clickTimes: this.state.clickTimes + 1 })
}
render() {
return (
<div>
<label>Type here:</label>
<input type="text" value={this.state.value} onChange={this.handleChange} />
<div style={{ marginTop: 20 }}>
<button onClick={this.handleClick}>Click-me</button>
Click times: {this.state.clickTimes}
</div>
</div>
)
}
}
ReactDOM.render(
<StateExample />,
document.getElementById('root')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>
有关更多信息,我建议阅读 ReactJS 文档中的 State and Lifecycle。
如果你初始化状态然后在构造函数中做
constructor(){
super();
let personProfile = this.state = {
name : "Bob",
skill : "Art Designer",
location : "LA"
}
console.log(personProfile);
this.state= { name : "Frank" };//initialvalue
}
如果你想改变一些动作,那么制作一个方法(changeName
)并在渲染中像这样使用:
changeName(name){
this.setState({name})
}
render(){
let changeName = this.setState({ name : "Frank" });
return(
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
<li onClick={this.changeName.bind(this,"hello")} > change Me </li>
</ul>
</div>
);
}
如果您在 render()
方法中调用 setState
,您将创建无限循环,您可以使用 componentDidMount
.
class StateBasic extends React.Component {
constructor() {
super();
let personProfile = this.state = {
name: "Bob",
skill: "Art Designer",
location: "LA"
}
}
componentDidMount() {
setTimeout(() => {
this.setState({name: "Frank"});
}, 1000)
}
render() {
return (
<div className="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<ul>
<li> {this.state.name} </li>
<li> {this.state.skill} </li>
<li> {this.state.location} </li>
</ul>
</div>
);
}
}
ReactDOM.render( <
StateBasic / > ,
document.getElementById('container')
)
<script src="https://unpkg.com/react@16.0.0/umd/react.development.js"></script>
<div id="container"></div>
<script src="https://unpkg.com/react-dom@16.0.0/umd/react-dom.development.js"></script>