2 回答

TA贡献2011条经验 获得超2个赞
this.data没有定义。您可以使用访问状态中设置的数据this.state.data
请确保this.props.location.state.data不为空
class A extends React.Component {
state = {
data: {}
};
componentDidMount() {
// this.data = this.props.location.state.data; => not required.
this.setState({
data: this.props.location.state.data
});
}
render() {
return ( <
div > {
Object.keys(this.state.data).map((key, index) => ( <
p key = {
index
} > value is {
this.state.data[key]
} < /p>
))
}
hello <
/div>
);
}
}

TA贡献1809条经验 获得超8个赞
从状态中获取数据而不是this.data因为它不会在this.data更新时触发渲染。也{}用作默认值
class A extends React.Component {
state = {
data: {}
};
componentDidMount() {
const data = {
id: 1,
userName: "ABDXY",
date: "01/12/2020",
time: "21:00"
};
this.setState({ data });
}
render() {
const { data } = this.state;
return (
<div>
{Object.keys(data).map((key, index) => (
<p key={index}> value is {data[key]}</p>
))}
hello
</div>
);
}
}
export default A;
添加回答
举报