首先,所有相关代码(单击该文件的完整源代码的文件名).
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>;
}
};
Run Code Online (Sandbox Code Playgroud)
authentication.js (减速器)
if (action.type === 'AUTHENTICATION.LOGIN_SUCCESS') {
return initialState.merge({
isAuthenticated: true,
token: action.data.token,
user: action.data.user
});
}
Run Code Online (Sandbox Code Playgroud)
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
}
};
};
Run Code Online (Sandbox Code Playgroud)
流程很简单:
<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
Run Code Online (Sandbox Code Playgroud)
预期的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
Run Code Online (Sandbox Code Playgroud)
问题是render具有正确的状态(后面的状态AUTHENTICATION.LOGIN_SUCCESS)并且componentWillReceiveProps具有旧状态(后面的状态AUTHENTICATION.LOGIN_REQUEST).
我是最后一次调用componentWillReceiveProps与该render方法具有相同的状态对象.
这是:
?
我写了所有这些调试跟踪/问题才记住componentWillReceivePropsAPI 是:
componentWillReceiveProps: function(nextProps) {}
Run Code Online (Sandbox Code Playgroud)
换句话说,我的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>;
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
772 次 |
| 最近记录: |