在我的react本机项目中的另一个组件中调度后执行函数

问题描述 投票:0回答:1

我想在另一个组件中发送某个函数后运行函数(承诺不是我的解决方案)例如我想在COMPONENT 1中调度THIS然后在Component中执行函数,负责启动动画对象

reactjs react-native redux react-redux
1个回答
0
投票

使用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,
        });
    }
}

`

© www.soinside.com 2019 - 2024. All rights reserved.