对道具(商店)进行更改
On props (store) change Do
我有一个名为参数的存储对象(React-Redux 应用程序使用连接与我的 JAVA API 交互)。
当我更新输入复选框等时,此对象在另一个组件中更新...
在我的新组件中,我有一个 "Save as Template" 按钮,它在经过一些处理后将此对象作为 Json 发送到我的 API。
我现在想在我的新组件中用任何奇怪的函数捕获对此对象(参数)的更改。
这样我就可以 运行 对其进行一系列 "if" 测试,查看是否缺少某些输入并相应地隐藏或变灰 "Save as template" 按钮。
这可能吗?
componentDidMount() {
}
和
componentWillReceiveProps() {
}
好像没什么用。
是的,这是我不得不使用几次的模式,我用 lodash.isEqual 来处理它。
import { isEqual as _isEqual } from "lodash"
假设您在存储中有一个参数对象,并且您正在 mapStateToProps 中的组件中查看它(或者您可以访问它):
const mapStateToProps = (state) => ({
parameters: state.someApp.parameters,
});
然后您可以检查您的 componentWillReceiveProps 中是否有任何更改:
componentWillReceiveProps(nextProps) {
if(!_isEqual(this.props.parameters, nextProps.parameters)) {
this.props.sendSomeRequestWithParameters(nextProps.parameters)
}
}
我有一个名为参数的存储对象(React-Redux 应用程序使用连接与我的 JAVA API 交互)。
当我更新输入复选框等时,此对象在另一个组件中更新...
在我的新组件中,我有一个 "Save as Template" 按钮,它在经过一些处理后将此对象作为 Json 发送到我的 API。
我现在想在我的新组件中用任何奇怪的函数捕获对此对象(参数)的更改。
这样我就可以 运行 对其进行一系列 "if" 测试,查看是否缺少某些输入并相应地隐藏或变灰 "Save as template" 按钮。
这可能吗?
componentDidMount() {
}
和
componentWillReceiveProps() {
}
好像没什么用。
是的,这是我不得不使用几次的模式,我用 lodash.isEqual 来处理它。
import { isEqual as _isEqual } from "lodash"
假设您在存储中有一个参数对象,并且您正在 mapStateToProps 中的组件中查看它(或者您可以访问它):
const mapStateToProps = (state) => ({
parameters: state.someApp.parameters,
});
然后您可以检查您的 componentWillReceiveProps 中是否有任何更改:
componentWillReceiveProps(nextProps) {
if(!_isEqual(this.props.parameters, nextProps.parameters)) {
this.props.sendSomeRequestWithParameters(nextProps.parameters)
}
}