简体   繁体   English

ReactJS,从数据库显示文本区域时,如何编辑文本区域并更新值?

[英]ReactJS, how to edit a textarea and update the value when displaying the textareas from database?

I am currently using node.js (for database storage using mySQL) and react.js (to display all text areas) 我目前正在使用node.js(用于使用mySQL存储数据库)和react.js(用于显示所有文本区域)

I'm not too sure how to do this, if I have a loop of textareas given from database, how would I be able to edit the value inside textarea and be able to retrieve that value in order to update it in my database when i click on 'Save data' button. 我不太确定如何执行此操作,如果我从数据库中给出了一个文本区域循环,那么当我在数据库中更新文本值时,我将如何编辑该值并能够检索该值来更新它点击“保存数据”按钮。

const WorkInProgressHLEDevDays = () =>{
return (
<tr>
<th> TEXTAREAs </th>
{this.state.WIPHLEsData.map((HLEDevDayTextAreas, i) => (
    <td key={i}>
    <textarea className="HLEDevDays"  onChange={this.handleChange} value={HLEDevDayTextAreas.HLEDevDays} spellcheck="false">

    </textarea>
    </td>

))}


</tr>

) } )}

There are numerous ways to implement this. 有很多方法可以实现这一点。 The easiest is to make your components uncontrolled, though it's the least recommended one according to facebook: 最简单的方法是使您的组件不受控制,尽管根据Facebook的建议,它是最不推荐的一种:

{this.state.WIPHLEsData.map((HLEDevDayTextAreas, i) => (
  <td key={i}>
    <textarea
      name={`HLEDevDays_${i}`}
      className="HLEDevDays"
      defaultValue={HLEDevDayTextAreas.HLEDevDays}
      spellcheck="false"
    />
  </td>
)}

As the name suggests, React doesn't manage the state of each textarea, but lets your browser do that via regular DOM mutations. 顾名思义,React不会管理每个文本区域的状态,但可以让您的浏览器通过常规的DOM突变来实现。 If you wrap this whole thing in a form , you will be able to read the values on your back-end (via the name attribute). 如果将整个内容包装成form ,您将能够读取后端的值(通过name属性)。

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

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