我正在尝试将表示组件与容器组件分开 . 我有 SitesTable
和 SitesTableContainer
. 容器负责触发redux操作以基于当前用户获取适当的站点 . 问题是在最初呈现容器组件之后,异步获取当前用户 . 这意味着容器组件不知道它需要在其 componentDidMount
函数中重新执行代码,这将更新要发送到 SitesTable
的数据 . 我想我需要在其中一个props(用户)更改时重新呈现容器组件 . 我该怎么做?
class SitesTableContainer extends React.Component {
static get propTypes() {
return {
sites: React.PropTypes.object,
user: React.PropTypes.object,
isManager: React.PropTypes.boolean
}
}
componentDidMount() {
if (this.props.isManager) {
this.props.dispatch(actions.fetchAllSites())
} else {
const currentUserId = this.props.user.get('id')
this.props.dispatch(actions.fetchUsersSites(currentUserId))
}
}
render() {
return <SitesTable sites={this.props.sites}/>
}
}
function mapStateToProps(state) {
const user = userUtils.getCurrentUser(state)
return {
sites: state.get('sites'),
user,
isManager: userUtils.isManager(user)
}
}
export default connect(mapStateToProps)(SitesTableContainer);
5 回答
您必须在
componentWillReceiveProps
方法中添加条件 .这就是组件的外观
UPDATE (React 17)
看起来
componentWillReceiveProps
将在React 17中删除 . 您可以使用componentDidUpdate
而不是componentWillReceiveProps
来实现相同的功能 .新示例使用fast-deep-equal而不是
JSON.stringify()
来比较对象 .由于错误和不一致,
ComponentWillReceiveProps()
将来会被弃用 . 在道具更改上重新渲染组件的替代解决方案是使用ComponentDidUpdate()
和ShouldComponentUpdate()
.每当组件更新时调用
ComponentDidUpdate()
并且如果ShouldComponentUpdate()
返回true(如果未定义ShouldComponentUpdate()
,则默认返回true
) .通过在其中包含条件语句,仅使用
ComponentDidUpdate()
方法可以实现相同的行为 .If one attempts to set the state without a conditional or without defining ShouldComponentUpdate() the the component will infinitely re-render
我认为这是你需要的事件 . 只要您的组件通过道具收到某些内容,就会触发
componentWillReceiveProps
. 从那里你可以检查然后做你想做的事 .我建议看一下我的这个answer,看看它与你在做什么有关 . 如果我理解你真正的问题,那就是新的道具 .
您的代码的这一部分:
不应该在组件中触发,应在执行第一个请求后处理 .
从redux-thunk看一下这个例子:
您不一定要使用redux-thunk,但它可以帮助您推理类似这样的场景并编写匹配的代码 .
您可以使用随道具更改的
KEY
唯一键(数据组合),并使用更新的道具重新呈现该组件 .