javascript - 无法读取未定义的属性“错误”(redux,react)
问题描述
我目前正在开发一个社交应用程序。我尝试了很多不同的传递参数的方法。任何帮助表示赞赏...代码如下
在这里,我在 componentWillReceiveProps 中遇到了 nextProps 的一些问题
我在反应中从经典组件转换为功能组件
在经典组件中,nextProps 实际上也可以使用
class signIn extends Component {
constructor() {
super();
this.state = {
email: "",
password: "",
errors: {},
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.UI.errors) {
this.setState({ errors: nextProps.UI.errors });
}
}
handleSubmit = (event) => {
event.preventDefault();
this.setState({
loading: true,
});
const userData = {
email: this.state.email,
password: this.state.password,
};
this.props.SignInUserAction(userData, this.props.history);
};
handleChange = (event) => {
this.setState({
[event.target.name]: event.target.value,
});
};
render() {
const {
classes,
UI: { loading },
} = this.props;
const { errors } = this.state;
return (
....
);
但它在功能组件中不起作用。
错误:无法读取未定义的属性“错误”。
const SignIn = ({ classes, UI: { loading } }) => {
// const { email, password, errors, handleSubmit, handleChange } = userSignIn();
const history = useHistory();
const [input, setInput] = useState({ email: "", password: "" });
const [errors, setErrors] = useState({});
useEffect(() => {
componentWillReceiveProps(errors);
}, [errors]);
const handleSubmit = (event) => {
if (event) event.preventDefault();
const userData = { email: input.email, password: input.password };
SignInUserAction(userData, history);
};
const handleChange = (event) => {
const { target } = event;
const { name, value } = target;
setInput({...value, [name]: value });
};
const componentWillReceiveProps = (nextProps) => {
if (nextProps.UI.errors) {
setErrors({ errors: nextProps.UI.errors });
}
};
return (
...
)
};
SignIn.propTypes = {
classes: PropTypes.object.isRequired,
signInUserAction: PropTypes.func.isRequired,
user: PropTypes.func.isRequired,
UI: PropTypes.func.isRequired,
};
const mapStateToProps = (state) => ({
user: state.user,
UI: state.UI,
});
const mapActionsToProps = { SignInUserAction };
export default connect(
mapStateToProps,
mapActionsToProps
)(withStyles(theme.styles)(SignIn));
解决方案
您正在传递errors
状态而不是任何实际的道具值。据我所知,您似乎应该将UI
道具传递给您的componentWillReceiveProps
实用程序。
const SignIn = ({ classes, UI }) => {
// const { email, password, errors, handleSubmit, handleChange } = userSignIn();
...
const [errors, setErrors] = useState({});
useEffect(() => {
componentWillReceiveProps(UI);
}, [UI]);
...
const componentWillReceiveProps = (uiProp) => {
if (uiProp?.errors) {
setErrors({ errors: uiProp.errors });
}
};
return (
...
)
};
SignIn.propTypes = {
classes: PropTypes.object.isRequired,
signInUserAction: PropTypes.func.isRequired,
user: PropTypes.func.isRequired,
UI: PropTypes.func.isRequired,
};
const mapStateToProps = (state) => ({
user: state.user,
UI: state.UI,
});
const mapActionsToProps = { SignInUserAction };
export default connect(
mapStateToProps,
mapActionsToProps
)(withStyles(theme.styles)(SignIn));
推荐阅读
- html - 如何在联系表 7 中设计验收标签?
- vue.js - 如何在 vue js 中对 datepicker 进行验证?
- c# - 如何捕获webservice返回的故障数据?
- azure-sql-database - 导入的 Azure SQL 数据库的大小始终为 250GB
- javascript - 如何仅计算选定的复选框 foreach
- javascript - 使用 VueJS 进行 SSO 更新
- python - re.search "TypeError: 预期的字符串或类似字节的对象"
- c - 递归函数 C?
- flutter - isEmpty 在 null 上被调用
- pyspark - 侧视图爆炸奇怪行为