使用react js和redux添加用户输入数据以显示在表中

时间:2018-11-18 09:57:17

标签: reactjs redux

有两个用户输入文本字段,我想使用redux在表中显示该数据。

<tbody>
    <tr key={key}>
     <td>
       <TextField
         id="FirstName"
         label="First Name"
         type="name"
         value={this.state.fname}                                           
        />
      </td>
      <td>
       <TextField
         id="LastName"
         label="Last Name"
         type="name"
         value={this.state.lastname}                                            
        />
     </td>
   </tr>

           添加图标     

1 个答案:

答案 0 :(得分:1)

Here is the solution,
Firstly To render it on a browser set the default values to your state variables.
constructor(props){
super(props);
this.state = {
fname : 'Foo ',
lastname : 'Bar',
}

}

Now, your code will get the value from state and it will render in DOM as you wanted. And use **defaultValue** in place of Value in input. Attribute 'value' is used when you are setting the values onChange but as you have already declared above in state , use **defaultValue** .

就像我在下面一样:-

<TextField
 id="FirstName"
 label="First Name"
 type="name"
 defaultValue={this.state.fname}                                           
  />