2 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
this.data沒有定義。您可以使用訪問狀態(tài)中設(shè)置的數(shù)據(jù)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貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
從狀態(tài)中獲取數(shù)據(jù)而不是this.data因?yàn)樗粫趖his.data更新時(shí)觸發(fā)渲染。也{}用作默認(rèn)值
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;
添加回答
舉報(bào)