简体   繁体   English

如何将props传递到从组件数组映射的组件?

[英]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.

相关问题 将道具传递到不同组件的映射数组 - Pass props to a mapped array of different components 如何将道具传递给带有样式组件的映射元素? - How to pass props to .mapped elements with styled-components? 如何将 JSX 映射逻辑与包含带有 props 的组件的映射数组分开? - How can I separate the JSX mapping logic from the mapped array containing components with props? 如何使用 useState 将映射的道具传递给组件? - How do I pass mapped props to a component using useState? 如何从组件传递道具 - how to pass props from a component 如何在反应中将道具从路由数组传递给组件 - how to pass props into component from route array in react 将组件作为道具传递给另一个组件 - Pass components as props to another component 如何在React Js中的路线映射数组上传递道具 - How to pass props on mapped array of routes in React Js 如何传递对象数组作为对组件的支持,然后将数组成员传递作为对嵌套组件的支持? - How can I pass down an array of objects as a prop to a component and then pass down the array's members as props to nested components? 将道具从映射组件传递到调用它的组件 - passing props from a mapped component to a component called into it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM