简体   繁体   English

通过道具传递功能

[英]Passing a function through props

i have a function that selects a row of a table, this function is below, it is within my component of SupplierSearchComponent 我有一个选择表行的函数,该函数在下面,它在我的SupplierSearchComponent组件内

   const handleAddressCheck = item => {
    const itemIndex = supplierAddress.findIndex(supplierItem => supplierItem.id === item.id);
    let newAddresses;
    if (itemIndex >= 0) {
        //the reasoning behind using a filter is that splice will not return an updated array, filter however does
        newAddresses = supplierAddress.filter(x => x.id !== item.id);
    } else {
        newAddresses = [...supplierAddress, item];
    }
    setSupplierAddress(newAddresses);
    props.onSelectAddress(newAddresses);

    console.log('this is the item => ', newAddresses);
};

However i also pass this function into another component as 但是我也将此函数传递给另一个组件

  const onSelectedAddressesChange = (addresses) => {
    props.setFieldValue('supplierAddresses', addresses);
};

I then pass the value of this into another component as 然后,我将此值传递给另一个组件

     <SearchSuppliers
       name="supplierSearch"
       label="Supplier Search"
       id="supplierSearch"
       onSelectAddress={onSelectedAddressesChange}
                        />

then from search suppliers i pass the prop into the original component of 然后从搜索供应商那里我将道具传递到

<SupplierSearchComponent
            onSelectAddress={props.onSelectAddress}
        />

Now finally within my SupplierSearchComponent i should be able to pass this prop as 现在终于在我的SupplierSearchComponent中,我应该能够通过

  <SupplierContactDetails
     onSelectAddress{props.onSelectAddress}
                            />

However within my ContactDetails i cannot actually console.log(onSelectAddress) as it will result as undefined, how could i successfully pass this function or rather its result through? 但是,在我的ContactDetails中,我实际上不能console.log(onSelectAddress),因为它将导致未定义,我如何才能成功地传递此函数,或者将其结果传递给我?

我想您在最后一个组件“ SupplierContactDetails”传递道具时跳过了“ =”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM