当 prop 从视图组件外部更改时,如何使用 recompose 重新渲染组件?
const Message = ({msg}) => <div>The prop has a value of <b>{msg}</b>.</div>
const App = Recompose.withProps(() => {
let msg = 'One'
// this async update doesn't work because nothing triggers a rerender
setTimeout(() => {msg = 'Two'}, 500)
return {msg}
})(Message)
ReactDOM.render(<App/>, document.getElementById('app'))
渲染此组件时,它显示值为One,但在 500 毫秒后不会更改为Two,即使它更改了 prop。
这里setTimeout
简化了在实际用例中我订阅 websocket 服务器并且当消息被推送时,消息组件被更新的情况。