我正在尝试在 React 中创建一个 Form 组件,有点像Formik,但更简单。
我的想法是onChange为所有孩子添加一个处理程序。我正在这样做children.map()。它有效,但是我收到一个关键警告
Warning: Each child in a list should have a unique "key" prop.
Run Code Online (Sandbox Code Playgroud)
我知道没有办法抑制这种情况,所以也许有更好的方法来创建这个表单组件?<input>另外,如果不是直系孩子,我应该如何处理这种情况?
编辑:我知道如何避免这个问题,我主要想要解决这个问题的最佳方法,包括嵌套输入的情况。
这是我想如何使用它:
<Form>
<label htmlFor="owner">Owner</label>
<input
type="text"
name="owner"
id="owner"
/>
<label htmlFor="description">Description</label>
<input
type="text"
name="description"
id="description"
/>
<input
type="submit"
value="Submit"
/>
</Form>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import React from 'react';
class Form extends React.Component {
constructor(props) {
super(props);
this.state = {}
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
const target = event.target;
const value =
target.type …Run Code Online (Sandbox Code Playgroud) React/JSX 新手,请耐心等待。我有一个返回对象的 API,如下所示
var currObj = {
SKU : 'Test_SKU_120934',
category : 'Home',
inventory_status : 1.
}
Run Code Online (Sandbox Code Playgroud)
我想迭代这个对象并返回一个 html 无编号列表,如下所示
<ul>
<li> <strong>SKU</strong> : 'Test_SKU_120934'</li>
<li> <strong>category</strong>: 'Home' </li>
<li> <strong>inventory_status</strong> : 1 </li>
</ul>
```
I can accomplish this in normal Javascript using 'for...in' loop. However, that loop doesn't return anything. How do I write statement below?
```
render () {
return (
);
}
```
Run Code Online (Sandbox Code Playgroud)