Reactjs 如何将td contentEditable={true}中的e.target.textContent传递给其他组件?
我是一个非常新的反应。我正在使用MERN处理一个表的基本积垢 一切正常,但我想发出put请求以编辑表的值 到目前为止,我试图做的是编辑表本身的内容,方法如下:Reactjs 如何将td contentEditable={true}中的e.target.textContent传递给其他组件?,reactjs,html-table,components,Reactjs,Html Table,Components,我是一个非常新的反应。我正在使用MERN处理一个表的基本积垢 一切正常,但我想发出put请求以编辑表的值 到目前为止,我试图做的是编辑表本身的内容,方法如下: <td onInput={(e) => e.target.textContent} contentEditable={true} suppressContentEditableWarning={true}>{informacion.responsable}</td> <td onInput={(e) =
<td onInput={(e) => e.target.textContent} contentEditable={true} suppressContentEditableWarning={true}>{informacion.responsable}</td>
<td onInput={(e) => e.target.textContent} contentEditable={true} suppressContentEditableWarning={true}>{informacion.noSello}</td>
<td onInput={(e) => e.target.textContent} contentEditable={true} suppressContentEditableWarning={true}>{informacion.identificadorEquipo}</td>
e.target.textContent}contentEditable={true}suppressContentEditableWarning={true}>{informacion.responsable}
e、 target.textContent}contentEditable={true}suppressContentEditableWarning={true}>{informacion.noSello}
e、 target.textContent}contentEditable={true}suppressContentEditableWarning={true}>{informacion.identificatedorequipo}
事实上,现在通过console.log,我可以看到(e.target.textContent)
然后…在这个表结构出现的同一个组件中,我有另一个组件(编辑按钮组件)。此按钮的作用是执行put请求,但我需要我的
中的e.target.textContent值,以便将它们作为json插入
<EditButton value1={td1} value2={td2} value3={td3}/>
1-如何做到这一点
2-这种方法有什么坏处吗
多亏了“一切为了你”的时间因为你对这一点非常陌生,现在是你开始学习状态
的时候了。首先,您必须导入状态挂钩:从React导入{useState}代码>
因此,在这种情况下,基本上可以将状态变量声明为:
const[content,setContent]=useState(“”)代码>
现在,您将拥有一个存储e.target.textContent
的content
状态,您可以通过setContent
setter设置该状态
现在,您可以将td
组件编写为:
setContent(e.target.textContent)}contentEditable={true}suppressContentEditableWarning={true}>{informacion.responsable}
这将用e.target.textContent
设置您的内容
状态。现在,如果您想将其传递给EditButton
组件,您可以这样做:
我会试试。谢谢。我经常忘记这里的一切都是关于状态的,如果我有60个td
…我应该使用useState、useReducer还是其他什么?@Maykelconterascamacho好吧,这取决于你和你的编程逻辑。但是,如果您必须为此使用60个状态,您可能需要后退一步,真正思考如何更好地构造您的应用程序。我将要使用的表有60个字段..不能从这个事实运行