简体   繁体   English

React - 传递给子组件时道具的值未准备好

[英]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.

相关问题 反应状态作为道具传递给子组件未显示 - React state passed as prop to child component not showing 当传递给反应中的功能组件时,Prop 是未定义的 - Prop is undefined when passed to functional component in react 传递给子组件的事件处理程序(prop)不能称为react native - Event Handler (prop) passed to child component cannot be called react native React state 作为道具传递,不会在子组件中更新 - React state passed as prop does not update in child component 通过 prop 传递给子组件的 Function 在调用时不是 function - Function passed via a prop to child component is not a function when called 作为prop传递的React组件的行为 - Behavior of a React component passed as prop 如何更改作为列传递给 React Table 的子组件道具的值? - How to change the value of a prop of Sub Component passed as a column to React Table? 当作为道具传递时,组件在道具更改时卸载 - Component unmounts on prop change when passed as prop 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 测试通过酶的某个prop值时组件是否不呈现 - Test that a component does not render when passed a certain prop value with enzyme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM