Reactjs 使用ShouldComponentUpdate()时的一步延迟
在我的ReactJS项目中,我有一个Reactjs 使用ShouldComponentUpdate()时的一步延迟,reactjs,Reactjs,在我的ReactJS项目中,我有一个表和它的过滤器。我使用axious从Django REST主机获得了JSON文件。因此,我的主表有一个类: class MainTable extends React.Component { constructor(props) { super(props); this.state = { results: [], count: 0 }; } shouldComponentUpdate(nextP
表
和它的过滤器。我使用axious
从Django REST
主机获得了JSON
文件。因此,我的主表有一个类
:
class MainTable extends React.Component {
constructor(props) {
super(props);
this.state = {
results: [],
count: 0
};
}
shouldComponentUpdate(nextProps) {
console.log(this.props.link + " : " + nextProps.link);
return JSON.stringify(this.props.link) !== JSON.stringify(nextProps.link);
}
render() {
axios.get(this.props.link)
.then(res => {
this.setState({results: res.data.results});
this.setState({count: res.data.count});
});
return (
<div>
<div>
<Label> We got {this.state.count} elements in our database. </Label>
</div>
<div>
<Table hover striped bordered responsive size="sm">
<thead>
<tr>
<th>Name</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{this.state.results.map(result =>
<tr key={result.fileId}>
<td>{result.name}</td>
<td>{result.name}</td>
</tr>
)}
</tbody>
</Table>
</div>
</div>
);
}
}
它会进行两次数据检查,并仅在下一次检查后重新运行渲染。也许我做错了什么。我对
ReactJS
是新手,我认为这应该是一个简单的答案。总是在设置了新的状态实例后调用Render,因此如果将setState放在componentDidMount()中,它应该用新数据刷新。只设定一次状态
class MainTable extends React.Component {
...
componentDidMount() {
axios.get(this.props.link)
.then(res => {
this.setState({results: res.data.results, count: res.data.count});
});
}
componentWillReceiveProps(nextProps) {
axios.get(nextProps.link)
.then(res => {
this.setState({results: res.data.results, count: res.data.count});
});
}
...
render() {
return ...
}
始终在设置新状态实例后调用Render,因此如果将setState放在componentDidMount()中,它将使用新数据刷新。只设定一次状态
class MainTable extends React.Component {
...
componentDidMount() {
axios.get(this.props.link)
.then(res => {
this.setState({results: res.data.results, count: res.data.count});
});
}
componentWillReceiveProps(nextProps) {
axios.get(nextProps.link)
.then(res => {
this.setState({results: res.data.results, count: res.data.count});
});
}
...
render() {
return ...
}
你不应该在渲染的中间设置状态。axios请求应处于操作中,并且应通过Redux/mobx/flux或您选择的状态管理器填充数据。如果不想这样做,可以在componentWillMount/componentDidMount中获取数据,然后在那里设置状态。即使没有状态管理器,也应该在componentDidMount步骤中进行api调用和新的设置状态。在componentWillMount中这被认为是一种不好的做法:如果我将我的
axios
放入render()
而不使用shouldComponentUpdate
,它将每秒击中我的主机几次,因此我想更改它。如果我使用“代码>组件DeNoMultUnter())/代码>,我的<代码>主代码< /代码>在发送新的道具后不会刷新新的数据。您不应该在渲染中间设置状态。axios请求应处于操作中,并且应通过Redux/mobx/flux或您选择的状态管理器填充数据。如果不想这样做,可以在componentWillMount/componentDidMount中获取数据,然后在那里设置状态。即使没有状态管理器,也应该在componentDidMount步骤中进行api调用和新的设置状态。在componentWillMount中这被认为是一种不好的做法:如果我将我的axios
放入render()
而不使用shouldComponentUpdate
,它将每秒击中我的主机几次,因此我想更改它。如果我使用componentDidMount()
,我的MainTable
在我向其发送新道具后不会刷新新数据。但它仍然只加载我的表一次。当我使用过滤器更改链接时,我的表
将加载相同的起始数据。我主要使用的是
,而link是一个我用过滤器更改的对象,用于请求REST
数据。好吧,你在帖子中没有提到它,看看componentWillReceiveProps,每次新的道具传递到组件时都会调用它,我会编辑我的帖子:对不起,在以后的帖子中会更精确。答案是Ty。没问题,请深入查看此页面:它可以处理您使用Reactor可能遇到的大多数问题,但它仍然只加载我的表一次。当我使用过滤器更改链接时,我的表
将加载相同的起始数据。我主要使用的是
,而link是一个我用过滤器更改的对象,用于请求REST
数据。好吧,你在帖子中没有提到它,看看componentWillReceiveProps,每次新的道具传递到组件时都会调用它,我会编辑我的帖子:对不起,在以后的帖子中会更精确。答案是Ty。没问题,请深入查看此页面:它可以处理使用react可能遇到的大多数问题