简体   繁体   English

如何在反应中仅渲染一个生成的孩子的孩子组件

[英]how to render child component of only one generated child in react

In a React app, I have a component to display the results of a search called ResultsContainer. 在React应用程序中,我有一个组件来显示名为ResultsContainer的搜索结果。 I am generating children by mapping a prop that has been passed down from a parent and creating a list element from each item in the array. 我通过映射从父代传递过来的prop并从数组中的每个项目创建一个list元素来生成子代。 Each of these children has a component that I (DetailsContainer) want to render when a button is clicked, and ONLY for the child that had the button clicked. 这些子项中的每个子项都具有一个我(DetailsContainer)要在单击按钮时呈现的组件,并且仅针对单击该按钮的子项。 As of now, the component renders for all of these children. 到目前为止,该组件将为所有这些子代渲染。 I am not sure how to target the individual child so that the component only renders for that specific one. 我不确定如何针对单个孩子,以便该组件仅针对该特定孩子进行渲染。

ResultsContainer: 结果容器:

var React = require('react');
var DetailsContainer = require('./DetailsContainer.jsx');

var ResultsContainer = React.createClass ({
  render: function () {
    var queryType = this.props.queryType;
    var details = this.props.details;
    var that = this;

    if (this.props.results == null) {
      var searchResult = "Enter search terms"
    } else {
      var searchResult = this.props.results.map(function(result, index){
      //if artist search
        if (queryType == "artist") {
         if (result.type == "artist") {
           return <li className="collection-item" key={index}> {result.title} <button onClick={that.props.handleDetailClick}>Get details</button> <DetailsContainer details={details} /> </li> ;
         }
        }
      });
    };

    return (
      <div className="collection">
        {searchResult}
      </div>
    );
  },
});

module.exports = ResultsContainer;

When the button is clicked, I want to render the DetailsContainer component for that child ONLY, not all of them. 单击按钮时,我只想为那个孩子渲染DetailsContainer组件,而不是全部。 How could I do this? 我该怎么办?

DetailsContainer: 详细信息容器:

var React = require('react');

var DetailsContainer = React.createClass({

  render: function(){
    if (this.props.details !== null) {
      var detailsDisplay = "deets"
    }
    return (
        <div className="detailsDisplay">
          {detailsDisplay}
        </div>
      );
  },

});

module.exports = DetailsContainer;

Here is handleDetailClick a function being passed down from the parent as a prop: 这是handleDetailClick作为父项从父级传递过来的函数:

handleDetailClick: function() {
    this.setState({details: "details"});
},

Also, in ResultsContainer, I could not figure out how to use .bind() to be able to use this.props.handleDetailClick from inside the callback of the .map, so i just created a variable "that" and set it equal to "this" outside the function. 另外,在ResultsContainer中,我无法弄清楚如何使用.bind()来从.map的回调中使用this.props.handleDetailClick,所以我只是创建了一个变量“ that”并将其设置为等于函数“外部”。 Is this bad practice? 这是不好的做法吗? What is the proper way to do it using .bind()? 使用.bind()的正确方法是什么? Thanks so much. 非常感谢。

First off map has an option to pass this. 首先关闭地图可以通过此选项。 map(function(item){}, this); map(function(item){},this); You have at least two options to limit the list. 您至少有两个选择来限制列表。 One is to use state like so; 一种是像这样使用状态。

 render() { let useList; if (this.state.oneSelected) useList = this.props.list[this.state.oneSelected]; else useList = this.props.list; let useListing = useList.map(function(item){}, this); return ({useListing}) } 

The other option is to pass the selected item to the parent this.props.selectOne(item.id) and have the parent return just the one item as a list. 另一种选择是将所选项目传递给父项this.props.selectOne(item.id),并使父项仅返回一个项作为列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM