gpt4 book ai didi

redux - componentWillReceiveProps 状态与 redux 状态更新后的渲染状态不同

转载 作者:行者123 更新时间:2023-12-04 12:40:06 28 4
gpt4 key购买 nike

首先,所有相关代码(单击文件名可查看该文件的完整源代码)。

LoginView.js

LoginView = class extends React.Component {
handleLogin = (email, password) => {
this.props.authenticationActionCreator.login(email, password);
};

componentWillMount () {
console.log('componentWillMount', 'this.props.isAuthenticated', this.props.isAuthenticated);
}

componentWillReceiveProps () {
console.log('componentWillReceiveProps', 'this.props.isAuthenticated', this.props.isAuthenticated);
}

render () {
let {
errorMessage,
isAuthenticating
} = this.props;

return <div>
<p>this.props.isAuthenticated: {this.props.isAuthenticated ? 'true' : 'false'}</p>
<button onClick={() => {
this.handleLogin('gajus@applaudience.com', 'nosyte');
}}>Login</button>
</div>;
}
};

authentication.js ( reducer )

if (action.type === 'AUTHENTICATION.LOGIN_SUCCESS') {
return initialState.merge({
isAuthenticated: true,
token: action.data.token,
user: action.data.user
});
}

authenticationActionCreator.js

authenticationActionCreator.loginSuccess = (token) => {
let decodedToken;

// @todo Handle failure to decode token.

decodedToken = jwtDecode(token);

localStorage.setItem('token', token);

return {
type: 'AUTHENTICATION.LOGIN_SUCCESS',
data: {
token,
user: decodedToken.user
}
};
};

流程很简单:
  • 用户打开页面。
  • 用户点击<button />调用 authenticationActionCreator.login .
  • console.log输出是:

    componentWillMount this.props.isAuthenticated true
    action AUTHENTICATION.LOGIN_REQUEST @ 16:52:50.880
    componentWillReceiveProps this.props.isAuthenticated true
    componentWillReceiveProps this.props.isAuthenticated false
    action AUTHENTICATION.LOGIN_SUCCESS @ 16:52:51.975

    预期 console.log输出是:

    componentWillMount this.props.isAuthenticated true
    action AUTHENTICATION.LOGIN_REQUEST @ 16:52:50.880
    componentWillReceiveProps this.props.isAuthenticated false
    action AUTHENTICATION.LOGIN_SUCCESS @ 16:52:51.975
    componentWillReceiveProps this.props.isAuthenticated true

    问题是 render具有正确的状态( AUTHENTICATION.LOGIN_SUCCESS 之后的状态)和 componentWillReceiveProps具有旧状态( AUTHENTICATION.LOGIN_REQUEST 之后的状态)。

    我是给 componentWillReceiveProps 的最后一个电话拥有与 render 相同的状态对象方法。

    这是:
  • 一个错误
  • 我做错了
  • 我的期望是错误的

  • ?

    最佳答案

    我花了我写所有这些调试跟踪/问题来记住 componentWillReceiveProps API 是:

    componentWillReceiveProps: function(nextProps) {}

    换句话说,我的 LoginView.js示例应该是:

    LoginView = class extends React.Component {
    handleLogin = (email, password) => {
    this.props.authenticationActionCreator.login(email, password);
    };

    componentWillReceiveProps (nextProps) {
    console.log('componentWillReceiveProps', 'nextProps.isAuthenticated', nextProps.isAuthenticated);
    }

    render () {
    let {
    errorMessage,
    isAuthenticating
    } = this.props;

    return <div>
    <p>this.props.isAuthenticated: {this.props.isAuthenticated ? 'true' : 'false'}</p>
    <button onClick={() => {
    this.handleLogin('gajus@applaudience.com', 'nosyte');
    }}>Login</button>
    </div>;
    }
    };

    关于redux - componentWillReceiveProps 状态与 redux 状态更新后的渲染状态不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34841013/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com