2 回答
TA贡献1790条经验 获得超9个赞
假设你有一个子组件 Child,你想将数据存在 store 下的 child。
当前的 store 状态是
{
// other data
child: {
// child data
}
}
在 redux 的世界里, store 的更改必须由 reducer function 来更改,此时就需要发送一个类似 resetChildData 的action,简写如下
export resetChildData = () => {
type: 'REST_CHILD_DATA',
}
然后需要书写对应的 reducer 来了真正的清除数据
const INIT_STATE = {}
export const childReducer = (state, action) => {
switch(action.type) {
case 'REST_CHILD_DATA':
return INIT_STATE
default:
return state
}
}
注意此时的 rootReducer 需要绑定这个 childReducer
const rootReducer = {
child: ChildReducer,
}
至此,在 Child 组件中,将 action 通过 connect 函数传入
import resetChildData from '../actions/child'
Class Child extends Component {
}
export default connect(
null,
{
resetChildData,
}
)(Child)
在组件任何地方就可以使用 this.props.resetChildData 来清空 store 下的 child
添加回答
举报
