Javascript Can';t将全局状态从reduxForm设置为本地状态
在用户设置中,我使用Redux表单中的Javascript Can';t将全局状态从reduxForm设置为本地状态,javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,在用户设置中,我使用Redux表单中的复选框。它工作得很好,但在与本地状态一起使用以更新文本时会出现问题 我正在尝试将样式添加到而不是`。因此,我必须将设计与本地州进行切换,如下所示: constructor(props) { super(props); this.state = { isSex: true }; } onToggle = () => { const { isSex } = this.state; this.
复选框
。它工作得很好,但在与本地状态一起使用以更新文本时会出现问题
我正在尝试将样式添加到
而不是`。因此,我必须将设计与本地州进行切换,如下所示:
constructor(props) {
super(props);
this.state = {
isSex: true
};
}
onToggle = () => {
const { isSex } = this.state;
this.setState({
isSex: !isSex
});
};
renderCheckBox() {
const { isSex } = this.state;
if (isSex) {
return (
<div>
<i class="fas fa-male"></i> male
</div>
);
} else {
return (
<div>
<i class="fas fa-female"></i> female
</div>
);
}
}
render() {
return (
<div className="formItem">
<p>Sex</p>
<div className="checkToggle">
<label
className="btn"
htmlFor="is_sex"
onClick={() => this.onToggle()}
>
{this.renderCheckBox()}
</label>
<Field
name="is_sex"
id="is_sex"
component="input"
type="checkbox"
/>
</div>
</div>
)
}
在我看来,问题在于:
function mapStateToProps({ user }) { // 1. <----issue
return {
initialValues: {
is_sex: user.is_sex
}
this.setState({ isSex: user.is_sex }); // 2. <----Big issue
};
}
如果此fetchUser
是一种更新状态/存储的方法,则必须分派一些操作。比如:
const mapDispatchToProps = dispatch => {
return {
fetchUser : ()=> dispatch(fetchAction())
};
}
const mapDispatchToProps = { fetchUser };
const mapDispatchToProps = dispatch => {
return {
fetchUser : ()=> dispatch(fetchAction())
};
}