Javascript 从Redux存储更新后,React组件无法重新渲染
有人能帮我弄清楚为什么我的组件在分派后没有更新吗Javascript 从Redux存储更新后,React组件无法重新渲染,javascript,reactjs,redux,jsx,conditional-rendering,Javascript,Reactjs,Redux,Jsx,Conditional Rendering,有人能帮我弄清楚为什么我的组件在分派后没有更新吗 function mapStateToProps(state) { const { isAuthenticated } = state return { isAuthenticated } } class LoginForm extends Component { handleSubmit(event) { event.preventDefault(); const { dispatch
function mapStateToProps(state) {
const { isAuthenticated } = state
return { isAuthenticated }
}
class LoginForm extends Component {
handleSubmit(event) {
event.preventDefault();
const { dispatch } = this.props
const credentials = this.state
dispatch(attemptLogIn(credentials));
}
// Dispatch works properly when API validates Login:
// Redux Log:
// nextState:{ authReducer: {isAuthenticated: true}}
render() {
const { isAuthenticated } = this.props;
return (
<div>
{ isAuthenticated && <div> Authenticated: True </div> }
// Does not render even after dispatch
<form onSubmit={this.handleSubmit}>
{... Form Stuff ...}
</form>
</div>
)
}
export default withRouter(connect(mapStateToProps)(LoginForm))
有人知道为什么我的组件不重新渲染吗?将MapStateTops函数更改为此
function mapStateToProps(state) {
const { isAuthenticated } = state.authReducer;
return { isAuthenticated };
}
在类LoginForm组件外部编写函数mapStateToProps表单。@KuldeepSaxena在此处发布代码是一个错误-我将重新编辑,我的MapStateTops实际上在组件外部-我刚刚错误地粘贴了我的示例:\a fiddle Please在上面工作,加载所有依赖项需要一些时间,例如您发布的唯一/根还原程序?或者您是否合并了更多?所以,如果有多个还原器,我必须始终指定从哪个还原器收集状态?是的,只定义您实际想要重新渲染组件的那些道具。为了更好的表现,一定要指定你的道具。例如,如果您有一个用户对象,并且只在渲染函数中使用它的特定属性,那么可以这样定义它:
username:state.user.name
而不是username:state.user
function mapStateToProps(state) {
const { isAuthenticated } = state.authReducer;
return { isAuthenticated };
}