[英]React - value of prop not ready when passed to child component
I have a page that displays the details of a meeting
and a button to edit it.我有一个显示
meeting
详细信息的页面和一个编辑它的按钮。 The button opens up a modal that has the meeting object passed to it like so:该按钮打开一个模式,该模式将会议 object 传递给它,如下所示:
<AddMeetingModal
show={isEditing}
cancel={closeEditModal}
edit={meeting}
/>
and in the model I have a `useEffect``which checks在 model 我有一个`useEffect`检查
useEffect(() => {
if(props.edit) {
//Fill input fields with values from meeting object
} else {
//Set all fields to blank for adding a new meeting
}
}, [props])
The error I'm getting comes when the inputs are trying to be filled cannot read value __ of undefined
obviously because on the first page, the meeting isn't loaded right away, so how can I wait for it to be loaded before calling the modal.当输入试图填充时,我得到的错误显然
cannot read value __ of undefined
因为在第一页上,会议没有立即加载,所以我怎么能在调用之前等待它被加载模态的。 I've tried using turnery operators, being more specific in the if statement above and just can't wrap my head around it.我尝试过使用车削运算符,在上面的 if 语句中更具体,只是无法理解它。
Also the meeting initially is being fetched via Redux hence why it isn't loaded intially.此外,会议最初是通过 Redux 获取的,因此最初没有加载它。
const meetingDetails = useSelector(state => state.meetingDetails)
const { loading, error, meeting } = meetingDetails
useEffect for Modal component模态组件的 useEffect
useEffect(() => {
if(!!props.edit) {
setMeetingHeld(props.edit.meeting_held)
setMeetingType(props.edit.meeting_type)
setAdd1(props.edit.location.address_line_1)
setAdd2(props.edit.location.address_line_2)
setAdd3(props.edit.location.address_line_3)
setTownCity(props.edit.location.town_city)
setCountyState(props.edit.location.county_state)
setPostCode(props.edit.location.post_code)
setCountry(props.edit.location.country)
setTime(props.edit.meeting_time)
setHeldWith(props.edit.held_with)
setDetails(props.edit.details)
setExistingConnector(props.edit.existing_connector)
setConnectorComments(props.edit.connector_comments)
setCompetitor(props.edit.competitor)
setMeetingReason(props.edit.meeting_reason)
setFollowUpActions(props.edit.follow_up_actions)
setFollowUpDate(props.edit.follow_up_date)
} else {
setMeetingHeld('scheduled')
setMeetingType('in_person')
setAdd1('')
setAdd2('')
setAdd3('')
setTownCity('')
setCountyState('')
setPostCode('')
setCountry('')
setTime('')
setHeldWith('')
setDetails('')
setExistingConnector(false)
setConnectorComments('')
setCompetitor('')
setMeetingReason('')
setFollowUpActions('')
setFollowUpDate('')
}
}, [props])
Try using a Promise
that only shows the modal when the meeting is ready.尝试使用仅在会议准备好时显示模式的
Promise
。 If you don't have the data right away, async
is the best way to go about this.如果您没有立即获得数据,
async
是 go 的最佳方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.