如何将数据从React/NextJS中的renderDetailPanel(即“row”)传递到函数“handleSubmit”?

mf98qq94  于 2023-03-29  发布在  React
关注(0)|答案(1)|浏览(121)

如何将数据从row传递到handleSubmit

const handleSubmit = async (e) => {
    e.preventDefault();

// do something with row data but how do I even get it here??

setExampleData(row.original.ExampleData) //this does not work

}



renderDetailPanel={({ row }) => (

//blah blah code, no idea where to take row to put into the function 

<Button onClick={handleSubmit}>Action something to {row.original.info}</Button>

)

我尝试将其放入renderDetailPanel中的一个表单槽中,但控制台显示为未定义。例如:

renderDetailPanel={({ row }) => (

//I put it here and hoped for the best but nothing came of it

<TextField
            autoFocus
            margin="dense"
            id="message"
            value={message}
            label="Message"
            onChange={(e) => {
              setMessage(e.target.value);
              setExampleData(row.original.ExampleData);  //over here
}}
/>

<Button onClick={handleSubmit}>Action something to {row.original.info}</Button>

)
oxcyiej7

oxcyiej71#

一个可能的解决方案是将row作为参数添加到handleSubmit()函数中:

const handleSubmit = (e, row) => {
    // do something with row here
}

然后使用onClick处理程序的匿名函数将row传递给handleSubmit()

<Button onClick={(e) => handleSubmit(e, row)}>...</Button>

相关问题