【发布时间】:2017-05-12 17:58:32
【问题描述】:
我创建了一个包含两个子组件的父类,其中一个子组件尝试将数据传递回父组件的状态。当我运行代码时,我没有收到任何运行时错误,但是将数据发送回父级的子级根本没有渲染(即使是最初)。所有组件都大写,所以这不是问题。我认为这与 OnClick 函数中的语法或我将道具传递回父级的方法有关。我是 React 新手,因此我们将不胜感激!
父类
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>
)
}
}
未呈现的子组件
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>
)
}
}
【问题讨论】:
-
请展示您如何导出和导入子组件。 :)
-
您是否遇到任何错误?是否到达
console.log("rendering");? -
console.log("rendering") 永远无法到达
标签: javascript reactjs