保持现在的方式真实
里面一个 componentDidMount 用类似的东西更新状态
componentDidMount
componentDidMount() { this.setState({ myList: global.MYCOMPANY.myList }); }
您还可以将提取移动到组件内的某些内容。在这里你可以开始利用其他反应生态系统,如redux / thunk或者只是DIY ......
componentDidMount() { fetch('/getList') .then((rs) => { this.setState({ myList: rs }); }) }
我使用箭头功能简洁。该 render 每次状态发生变化时,都会调用类中的方法。如果需要,您可以选择不返回任何内容,空列表甚至是加载图标。这仍然是一个相当入门级的解决方案或适用于小型项目,但在寻求向外扩展之前是一个很好的起点。
render