[英]Passing arguments to React component using ES6
I am trying to create a sortable list with the react.js library "react-sortable-hoc" ( https://github.com/clauderic/react-sortable-hoc ). 我正在尝试使用react.js库“ react-sortable-hoc”( https://github.com/clauderic/react-sortable-hoc )创建一个可排序列表。
In "SortableList" I map a function on each element of array which (should) create a "SortableElement" with the arguments key , index and value . 在“ SortableList”中,我在数组的每个元素上映射了一个函数,该函数(应该)使用参数key , index和value创建一个“ SortableElement”。 This is how "SortableElement" is defined: https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableElement/index.js 这是定义“ SortableElement”的方式: https : //github.com/clauderic/react-sortable-hoc/blob/master/src/SortableElement/index.js
SortableItem = SortableElement(({value,key}) =>
<Row>
<this.DragHandle/>
<ControlLabel>Question</ControlLabel>
<FormControl
componentClass="textarea"
placeholder="Enter question"
onChange={()=> {
console.log(key); //why undefined??
console.log(value);
}
}
/>
</Row>);
SortableList = SortableContainer(({items}) => {
return (
<div>
{items.map((value, index) =>
<this.SortableItem key={`item-${index}`}
index={index}
value={value}/>
)}
</div>
);
});
Unfortunately, key and index are always undefined, and I just don't understand why. 不幸的是, 键和索引总是不确定的,我只是不明白为什么。 If you are interested in the full code, please visit https://github.com/rcffc/react-dnd-test/blob/master/src/SortableComponent.js 如果您对完整代码感兴趣,请访问https://github.com/rcffc/react-dnd-test/blob/master/src/SortableComponent.js
Any help is appreciated. 任何帮助表示赞赏。
If you look at the source for react-sortable-hoc
, you'll see that when passing props in the render, index
is omitted. 如果您查看react-sortable-hoc
的源代码,则会看到在渲染器中传递道具时会省略index
。 Also, key
isn't passed in props. 另外, key
不会在道具中传递。 If you change your destructuring to log props in SortableItem
, you'll see an object containing just the value ('Question 1', 'Question 2', etc). 如果您将解构更改为在SortableItem
记录道具,则会看到仅包含该值的对象(“问题1”,“问题2”等)。 If you need to access the index or key, pass them as different props. 如果您需要访问索引或键,请将它们作为不同的道具传递。
eg 例如
SortableItem = SortableElement(({value,yourIndex}) =>
<Row>
<this.DragHandle/>
<ControlLabel>Question</ControlLabel>
<FormControl
componentClass="textarea"
placeholder="Enter question"
onChange={()=> {
console.log(yourIndex);
console.log(value);
}
}
/>
</Row>
);
SortableList = SortableContainer(({items}) => {
return (
<div>
{items.map((value, index) =>
<this.SortableItem key={`item-${index}`}
index={index}
value={value}
yourIndex={index} />
)}
</div>
);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.