我是ReactJS的新手,我试图了解state
和setState()
。使用setState()
我想更改名称,但我不确定在我的代码中应该在哪里调用setState()
方法:
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;
答案 0 :(得分:3)
如果您使用setState
方法拨打render()
,则会创建无限循环,而您可以使用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>
答案 1 :(得分:2)
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')
)
&#13;
<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>
&#13;
有关详情,建议您阅读ReactJS文档中的State and Lifecycle。
答案 2 :(得分:2)
如果初始化状态,则在构造函数中执行
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>
);
}