我想在另一个组件中发送某个函数后运行函数(承诺不是我的解决方案)例如我想在COMPONENT 1中调度THIS然后在Component中执行函数,负责启动动画对象
使用redux的解决方案是使用mapStateToProps
来收听商店的变化。
示例:userState = {isLoggedIn: false}
A - 发送loginSuccessAction
B - 更改reducer中的用户并将其设置为```LoggedIn:true````
C - 使用isLoggedIn的mapStateToProps
D - 改变听力组件中的任何内容
`
// Action
const loginSuccess = {type: loginSuccess }
//
//Reducer
export function reducer( state = initialState, action){
switch (action.type) {
case loginSuccess.type: {
return {
...state,
isFetching: false,
isLoggedIn: true,
errorMessage: '',
};
}
case logoutRequest.type: {
return initialState;
}
default:
return state;
}
}
}
// MapStatetoPros Other Component listening to isLoggedIn
function mapStateToProps(state) {
return {
isLoggedIn: state.user.isLoggedIn,
};
}
// When props change other component
componentWillReceiveProps(nextProps) {
if (this.props !== nextProps) {
this.setState({
isLoggedIn: nextProps.isLoggedIn,
});
}
}
`