私は以下のコードを持っていますが、私の状態は更新されません。 私はAngular http ajax-callを使用して、ユーザーが正しいかどうかを確認しています。新しいエラーメッセージをプロップとして渡すと、何も起こりませんが、コンポーネントはnextProps
からアクセスできるので、コンポーネントを受け取ってしまいます。反応状態/小道具は更新されません
私もちょうど{ this.props.error }
をレンダリングするconstructor
、componentWillReceiveProps
とshouldComponentUpdate
をスキップしようとしたが、それはどちらか動作しませんでした。
これはDOM初めてレンダリングするために、私のレンダリング機能である
// Some code
.then(function(response){
// Some code
}, function(response){
_this.renderLogin("User not found"); // Is sending the error-message to the function
});
// Some code
_this.renderLogin = function(error){
render(
<Login error={error} />,
document.getElementById("app")
);
};
_this.renderLogin("Standard");
これは、ログイン・コンポーネントです:任意の助けを事前に
class Login extends React.Component {
constructor(props){
super(props);
this.state = {
error: this.props.error
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.error !== this.state.error) {
this.setState({ error: nextProps.error });
console.log(nextProps.error); // User not found
console.log(this.state.error); // Standard
}else{}
}
shouldComponentUpdate(nextProps, nextState){
console.log(nextState.error); // User not found
console.log(nextProps.error); // User not found
console.log(this.state.error); // Standard
return true;
}
render(){
return(
<div>
{ this.state.error } // Always showing: 'Standard'
</div>
);
}
}
export default Login;
ありがとう!
感謝。それは理にかなっている :) – Gjermund