我有一個父組件,PlanList:陣營子組件的狀態不定
class PlanList extends Component {
constructor(props) {
super(props);
this.renderPlans = this.renderPlans.bind(this);
this.planListFilter = <PlanListFilter onChange={this.handleFilterChange.bind(this)} />
}
loadPlans() {
console.log(this.planListFilter);
// returns: Object {$$typeof: Symbol(react.element), key: null, ref: null, props: Object, _owner: ReactCompositeComponentWrapper…}
console.log(this.planListFilter.state);
// returns: undefined
// I expect it to return the state object i defined in the PlanListFilter constructor
// here I would apply the filters to the PlanTable
}
handleFilterChange(event) {
this.loadPlans();
}
render() {
return (
<div className="PlanList">
{this.planListFilter}
<PlanTable />
</div>
)
}
}
和子組件,PlanListFilter:
class PlanListFilter extends Component {
constructor(props) {
super(props);
this.state = {
search: '',
};
this.handleSearchChange = this.handleSearchChange.bind(this);
}
handleSearchChange(event) {
this.setState({search: event.target.value});
this.props.onChange(event);
}
render() {
return (
<FormControl type="text" placeholder="Search" onChange={this.handleSearchChange} value={this.state.search} />
);
}
}
當改變在FormControl的文本,該onChange
屬性按預期發射,但在父母對象中,子女的state
爲undefined
。我預計它會填充正確的狀態。
顯然,它不是在構造定義.. – webdeb
是的,它是: '構造(道具){ 超級(道具); this.state = { search:'', };' – Ben174
哦,我明白了,你正在做一些瘋狂的狗屎..它沒有怎麼反應的作品.. – webdeb