简体   繁体   English

反应子组件不渲染

[英]React Child Component Not Rendering

I have created a parent class that holds two child components, with one of the children trying to pass data back to the parent's state. 我创建了一个包含两个子组件的父类,其中一个子组件试图将数据传递回父组件的状态。 When I run the code, I do not get any runtime errors, but the child that is sending data back to the parent is not rendering at all (even initially). 在运行代码时,我没有遇到任何运行时错误,但是将数据发送回父级的子级根本没有呈现(即使是最初)。 All the components are capitalized so that is not the issue. 所有组件都大写,所以这不是问题。 I think it has something to do with the syntax in the OnClick function, or my method of passing the props back to the parent. 我认为这与OnClick函数中的语法有关,或者与将道具传递回父对象的方法有关。 I am new to React, so any help would be appreciated! 我是React的新手,所以我们将不胜感激!

Parent Class 家长班

  class DropdownParent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {propsForClassDropdown: []};
    this.getClasses = this.getClasses.bind(this);
  }

  getClasses(subjectTitle){
    var chosenSubject = Subjects.subjectTitle;
    this.setState({propsForClassDropdown: {chosenSubject}.classes})
  }


  render(){
    return(
    <div className = "DropdownContainer">
      <SubjectDropdown triggerListChange = {this.getClasses} />
      <ClassDropdown />     //this one renders properly
    </div>
    )
  }
}

Child component that is not rendering 未呈现的子组件

class SubjectDropdown extends React.Component {
  constructor(props) {
    super(props);
    this.state = { searchTerm: "", searchStarted: false };
    this.searchUpdated = this.searchUpdated.bind(this);
  }
  searchUpdated (term) {
    this.setState({searchTerm: term, searchStarted: true})
  }

  render(){
    console.log("rendering");
    const filteredSubjects = Subjects.filter(createFilter(this.state.searchTerm, KEYS_TO_FILTERS))
    return(
    <div>
      <div className = "SubjectDropdownContainer">
        <SearchInput className='search-input' onChange={this.searchUpdated} />
            <div className = 'SubjectDropdown'>
              {filteredSubjects.map(Subjects => {
                  return (
                    <div>
                    <Button className = "dropdownItem" key={Subjects.id} onClick={()=>this.props.triggerListChange(key)}>
                      {Subjects.subject.name}
                    </Button>
                    </div>
                  )
              })}
            </div>
      </div>
    </div>
    )
  }
}

In your onClick handler, you pass key to triggerListChange but key isn't defined anywhere. 在您的onClick处理程序中,您将key传递给triggerListChange,但是key没有在任何地方定义。

{filteredSubjects.map(Subjects => {
              return (
                <div>
                <Button className = "dropdownItem" key={Subjects.id} onClick={()=>this.props.triggerListChange()}>
                  {Subjects.subject.name}
                </Button>
                </div>
              )
          })} 

Assuming Subjects is within scope of the parent and child component, and Subject is an object, that should work. 假设Subjects在父级和子级组件的范围内,并且Subject是一个应该起作用的对象。

In the callback to your map function: filteredSubjects.map(Subjects => { the name of the Subject s parameter collides with your global Subjects object. Try changing that to: filteredSubjects.map(subject=> { and pass subject.id to triggerListChange . Ie; onClick={() => this.props.triggerListChange(subject.id) 在地图函数的回调中: filteredSubjects.map(Subjects => { Subject的参数名称与您的全局Subjects对象冲突。尝试将其更改为: filteredSubjects.map(subject=> {并将triggerListChange传递给triggerListChange 。即; onClick={() => this.props.triggerListChange(subject.id)

In your getClasses function: 在您的getClasses函数中:

getClasses(subjectTitle){
  var chosenSubject = Subjects.subjectTitle;
  this.setState({propsForClassDropdown: {chosenSubject}.classes})
}

Change var chosenSubject = Subjects.subjectTitle to accessing the property via bracket [] notation: Subjects[subjectTitle] because the id from your subject object might be a string. var chosenSubject = Subjects.subjectTitle更改为通过方括号[]表示法访问属性: Subjects[subjectTitle]因为主题对象的id可能是字符串。

Turns out my export statement in the parent component was incorrect. 原来我在父组件中的导出语句不正确。 Not sure why one of the components was still rendering though. 虽然不确定为什么其中一个组件仍在渲染。

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

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