为什么:无法读取未定义的 属性 'email'?反应-dom
Why: Cannot read property 'email' of undefined? React-dom
我需要在另一个组件中使用我的状态,我用这样的道具发送了它。
好的,这是我的 ./HandleForm 容器。
import React, { PureComponent } from "react";
import NotLoggedRegisterForm from "./NotLoggedRegisterForm";
export default class HandleForm extends PureComponent {
state = {
data: {
email: "",
password: "",
confirmPassword: ""
}
};
handleChange = e => {
this.setState({ ...this.state.data, [e.target.name]: e.target.value });
};
render() {
return (
<div>
<NotLoggedRegisterForm data={this.state.data} />
</div>
);
}
}
这是组件的一部分,而不是接收道具。
const { data } = props.data;
<Input
prefix={<Icon type="mail" style={{ color: "rgba(0,0,0,.25)" }} />}
type="email"
placeholder="Email"
name="email"
onChange={props.handleChange}
value={data.email}
/>
我收到错误代码:
TypeError
Cannot read property 'email' of undefined
NotLoggedRegisterForm
/src/pages/logged/not/registerForm/NotLoggedRegisterForm.js:38:24
35 | placeholder="Email"
36 | name="email"
37 | onChange={props.handleChange}
> 38 | value={data.email}
| ^
39 | />
40 | )}
41 | </FormItem>
改变
const { data } = props.data;
进入
const { data } = props;
在您的代码中
const { data } = props.data
错误。
因为props
包含data
,而props.data
不包含data
所以你可以这样改。
const data = props.data;
// or you may use "const {data} = props;"
<Input
prefix={<Icon type="mail" style={{ color: "rgba(0,0,0,.25)" }} />}
type="email"
placeholder="Email"
name="email"
onChange={props.handleChange}
value={data.email}
/>
我需要在另一个组件中使用我的状态,我用这样的道具发送了它。
好的,这是我的 ./HandleForm 容器。
import React, { PureComponent } from "react";
import NotLoggedRegisterForm from "./NotLoggedRegisterForm";
export default class HandleForm extends PureComponent {
state = {
data: {
email: "",
password: "",
confirmPassword: ""
}
};
handleChange = e => {
this.setState({ ...this.state.data, [e.target.name]: e.target.value });
};
render() {
return (
<div>
<NotLoggedRegisterForm data={this.state.data} />
</div>
);
}
}
这是组件的一部分,而不是接收道具。
const { data } = props.data;
<Input
prefix={<Icon type="mail" style={{ color: "rgba(0,0,0,.25)" }} />}
type="email"
placeholder="Email"
name="email"
onChange={props.handleChange}
value={data.email}
/>
我收到错误代码:
TypeError
Cannot read property 'email' of undefined
NotLoggedRegisterForm
/src/pages/logged/not/registerForm/NotLoggedRegisterForm.js:38:24
35 | placeholder="Email"
36 | name="email"
37 | onChange={props.handleChange}
> 38 | value={data.email}
| ^
39 | />
40 | )}
41 | </FormItem>
改变
const { data } = props.data;
进入
const { data } = props;
在您的代码中
const { data } = props.data
错误。
因为props
包含data
,而props.data
不包含data
所以你可以这样改。
const data = props.data;
// or you may use "const {data} = props;"
<Input
prefix={<Icon type="mail" style={{ color: "rgba(0,0,0,.25)" }} />}
type="email"
placeholder="Email"
name="email"
onChange={props.handleChange}
value={data.email}
/>