[英]How to pass props to a component mapped from an array of components?
I am new to React and sometimes struggle with the terminology. 我是React的新手,有时还难以使用术语。 My problem is that I am creating a ScrollableList
of components which are passed as an array prop to the ScrollableList
. 我的问题是我正在创建一个ScrollableList
组件,这些组件作为数组prop传递给ScrollableList
。 The ScrollableList
then maps each of the components inside a div and this does not allow me to pass props to the component such as 'handleChange'. 然后ScrollableList
映射div中的每个组件,这不允许我将props传递给组件,例如“ handleChange”。 Is there another way to do this that I don't know about? 还有另一种我不知道的方法吗?
ScrollableList rendering: ScrollableList呈现:
render() {
const startPosition = this.state.scrollPosition -
this.props.maxItemsToRender > 0
? this.state.scrollPosition - this.props.maxItemsToRender
: 0
const endPosition = this.state.scrollPosition +
this.props.maxItemsToRender >=
this.props.listItems.length
? this.props.listItems.length
: this.state.scrollPosition + this.props.maxItemsToRender
return (
<div className="react-scrollable-list" ref="list" style={this.props.style}>
<div
key="list-spacer-top"
style={{
height: startPosition * this.props.heightOfItem
}}
/>
//Where My Problem Begins
{this.props.listItems.slice(startPosition, endPosition).map(item => (
<div handleChange={this.handleChange}
className="react-scrollable-list-item"
key={'list-item-' + item.id}>
{item.content}
</div>
))}
<div
key="list-spacer-bottom"
style={{
height: this.props.listItems.length * this.props.heightOfItem -
endPosition * this.props.heightOfItem
}}
/>
</div>
)
}
The {item.content}
contains the component that I wish to pass the prop handleChange={this.handleChange}
to. {item.content}
包含我希望将handleChange={this.handleChange}
传递给的组件。 Is there a way to do this or is my design the problem? 有没有办法做到这一点,还是我的设计有问题?
You can extract item to separate function 您可以提取项目以分离功能
renderItem(item) {
const Content = item.content;
return (
<div
handleChange={this.handleChange}
className="react-scrollable-list-item"
key={item.id}
>
<Content foo="bar" someProp={9} />
</div>
);
}
render() {
const startPosition = this.state.scrollPosition -
this.props.maxItemsToRender > 0
? this.state.scrollPosition - this.props.maxItemsToRender
: 0
const endPosition = this.state.scrollPosition +
this.props.maxItemsToRender >=
this.props.listItems.length
? this.props.listItems.length
: this.state.scrollPosition + this.props.maxItemsToRender
return (
<div className="react-scrollable-list" ref="list" style={this.props.style}>
<div
key="list-spacer-top"
style={{
height: startPosition * this.props.heightOfItem
}}
/>
//Where My Problem Begins
{this.props.listItems.slice(startPosition, endPosition).map(item => this.renderItem(item))}
<div
key="list-spacer-bottom"
style={{
height: this.props.listItems.length * this.props.heightOfItem -
endPosition * this.props.heightOfItem
}}
/>
</div>
)
}
the this.handleChange function is reference to this component function . this.handleChange函数是对此组件函数的引用。
you can try this: 您可以尝试以下方法:
//Where My Problem Begins
{this.props.listItems.slice(startPosition, endPosition).map(item => (
<div handleChange={this..props.handleChange}
className="react-scrollable-list-item"
key={'list-item-' + item.id}>
{item.content}
</div>
))}
as you can see, i put the function pointing to a props which you can make a callback here 如您所见,我将函数指向一个道具,您可以在此处进行回调
使用handleChange={() => this.handeChange(item.content)}
而不是handleChange={this.handleChange}
来传递值{item.content}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.