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