简体   繁体   English

反应发送道具问题

[英]React sending props issue

I am using React Kendo grid like this:我正在像这样使用 React Kendo 网格:

      <Grid sortable={true}
            filterable={true}
            onDataStateChange={onDataStateChange}
            groupable={true}
            reorderable={true}
            pageable={true}
            data={result}
            {...dataState}>
            <GridColumn cell={CommandCell} title="Options"/>
            <GridColumn field="session_id" title="Session" filter='text'/>
            <GridColumn field="sn_zag_id" title="Service" filter='text'/>
      </Grid>

The CommandCell looks like this:命令单元看起来像这样:

const CommandCell = (props) => <MyCommandCell {...props}/>;

const MyCommandCell = (props) => {
      return <td className="k-command-cell">
        <div style={{marginTop:'2%'}}>
         <Button style={{width:'8vw',marginTop:'2%'}}
          onClick={()=> alert(props)}>
           <FcCancel/>Alert
          </Button>
          <Button style={{width:'8vw',marginTop:'2%'}}
          onClick={()=>toggleDialogPrilog()}>
           <AiFillFileAdd/> Add file
          </Button></>}
          </div> 
          { visible &&<Dialog onClose={toggleDialogPrilog} title={"Add file"}>
          <Prilog props={props}/>
          </Dialog>}
        </td>;
    };

So, my intention is, when I click on the Button 'Add' in the grid, I want the dialog to open and I want to send props to the 'Prilog' component.所以,我的意图是,当我单击网格中的“添加”按钮时,我希望打开对话框并将道具发送到“Prilog”组件。 Prilog component looks like this: Prilog 组件如下所示:

export default  function Prilog ({props}) {
  
    console.log(props);
    const [file, setFile] = useState();
 const handleSubmit = async(event) =>{
        console.log(event);
        event.preventDefault()
        let resp = await services.uploadSnPrilog(0,props,file.name,file.opis,file.base64);
        console.log(resp);
    
      }

 const handleSubmit = async(event) =>{
        console.log("Hello");
        ...some logic...
      }

return <>
    <DialogActionsBar>
        <div>
<p>{props.dataItem.sn_zag_id}</p>
            <form  onSubmit={handleSubmit}>
                <button className='appBar-containers'  type="submit">
                <FaUpload/> Add
                </button>
                <input type="file" style={{marginTop:'1vh', marginLeft:'1vw'}}></input>
            </form>
        </div>
    </DialogActionsBar>
    </>

}

But the thing is, when I click 'Add' on any row in the grid, the console.log in Prilog component logs all the props, from all visible rows, and not the row that I clicked.但问题是,当我在网格中的任何行上单击“添加”时,Prilog 组件中的 console.log 会记录所有可见行的所有道具,而不是我单击的行。 Also, the此外,

{props.dataItem.sn_zag_id} {props.dataItem.sn_zag_id}

writes always the first written 'sn_zag_id' from the first prop that was console logged. 始终写入控制台记录的第一个道具中的第一个写入的“sn_zag_id”。 On the other hand, if I click 'Alert', it console logs the props from only that row. 另一方面,如果我单击“警报”,它的控制台将仅记录该行的道具。 How can I send only the props of the clicked row to the Prilog component and not all props? 如何只将点击行的道具发送到 Prilog 组件而不是所有道具?

The console.log in the body of the component is showed every time the component re-renders, which happens every time a Parent component re-renders, so it's pretty likely that your handleSubmit triggers a re-render up in the tree, could be a global store dispatch or so.每次组件重新渲染时都会显示组件主体中的console.log ,这在每次父组件重新渲染时都会发生,因此您的handleSubmit很可能会触发树中的重新渲染,可能是全球商店派送左右。 If you don't want the Prilog that don't change their props to re-render you have to wrap them into React.memo() .如果您不希望不更改道具的Prilog重新渲染,您必须将它们包装到React.memo()中。

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

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