使用套接字监听服务器;Redux存储不断更新数千条数据记录。更新存储只需要几秒钟的时间,数千个对象通过操作进行调度。然而,使用redux connect函数通过mapStateToProps将状态映射到我的组件,似乎会将状态更改排入队列,并以大约每秒7-10条记录的速度更新组件的状态。这意味着React组件需要很长时间才能呈现。有什么解决方案可以加速这一过程吗?此外,确切的数据量总是在变化,并且没有固定的数据量。
下面是我的组件:
class TestComponent extends Component {
state = {};
componentDidMount() {
this.props.connectToSocket();
}
render() {
const { classes, width, people, vehicles, incidents, locations } = this.props;
return (
<div>
Hello
</div>
);
}
}
TestComponent.propTypes = {
classes: PropTypes.object.isRequired
};
const mapStateToProps = state => {
console.log(state);
return {
people: state.live.people,
vehicles: state.live.vehicles,
incidents: state.live.incidents,
locations: state.live.locations
}
};
const mapDispatchToProps = {
connectToSocket: connectToSocket
};
export default connect(mapStateToProps,mapDispatchToProps(TestComponent));
初始化套接字的操作在componentDidMount()函数中执行。然后,我可以在控制台中看到打印的状态,但是,它每秒打印大约7-10条新记录的更新。由于在很短的时间内对存储进行了超过5000次更新,将redux存储映射到组件的道具需要更长的时间,并且需要5分钟以上的时间来呈现组件。
有什么想法吗?
发布于 2019-01-12 03:31:53
通常,这里的答案涉及到某种形式的批处理:
有关更多想法和链接,请参阅Redux FAQ entry on reducing the number of store update events。
https://stackoverflow.com/questions/54148646
复制相似问题