【发布时间】:2020-08-09 03:58:09
【问题描述】:
大家好,我正在尝试将 prop 中的命令从父组件传递给子组件。问题是,单击我的按钮后,必须更改父组件中的状态才能打开预期的侧边栏。我似乎没有正确使用 onClick 函数末尾的 .then() 部分,因为我收到错误:× 未处理的拒绝(TypeError):无法读取未定义的属性“then”
儿童组件:
return (
<div
className="results-item"
onClick={async () => {
let place_detail;
try {
const URL = `https://maps.googleapis.com/maps/api/place/details/json?place_id=${places.place_id}&fields=name,rating,formatted_phone_number&key=
MYAPIKEY`;
const response = await axios.get(URL);
console.log(response.data);
place_detail = response.data.results;
} catch (error) {
console.log(error.message);
}
this.setState({ place_detail }).then(this.props.sideBarOpen);
}}
>
<ResultsItem
name={places.name}
image={Thumbnail}
rating={places.rating}
rating_total={places.user_ratings_total}
/>
</div>
);
父组件:
<Search sideBarOpen={() => setOpen(!open)} />
【问题讨论】:
标签: reactjs