gpt4 book ai didi

javascript - 在 React 中将数据从子级传递给父级

转载 作者:行者123 更新时间:2023-12-03 13:56:17 25 4
gpt4 key购买 nike

我在 React 中有 3 个组件,其中一个充当容器,它将我的子组件传递到表单中进行渲染。提交表单时,我想获取父组件中的每个子组件,循环遍历每个子组件,创建服务器期望的对象,然后将对象列表发送回服务器。我正在努力访问父组件中 onSubmit 函数中的子组件。

这是我的父组件

ParentFixturesComponent.js

class ParentFixturesComponent extends Component {

constructor() {
super();
this.state = {
numChildren: 0,
};
}

onAddMatch() {
this.setState({
numChildren: this.state.numChildren + 1
});
}

onSubmit(e) {
e.preventDefault();
// loop through the child components
// create a match object with them
// var match = {
// id: uuid(),
// title: uuid(),
// start: e.something,
// end: e.something,
// };
console.log("submit works");
}

render() {
const children = [];
for (let i = 0; i < this.state.numChildren; i += 1) {
children.push(<SingleMatchForm key={uuid()}/>)
}

return (
<Fixtures addMatch={this.onAddMatch.bind(this)} save={this.onSubmit.bind(this)} >
{children}
</Fixtures>
);
}

}

export default ParentFixturesComponent;

保存我的表单的组件,其中我的父组件渲染我的所有子组件

ChildFixturesContainer.js

class Fixtures extends Component {


constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
}

handleChange(name) {
this.console.log(this.props);
}

render() {
return (
<div className="tray tray-center">
<div className="row">
<div className="col-md-8">
<div className="panel mb25 mt5">
<div className="panel-heading">
<span className="panel-title">Fixtures</span>
</div>
<div className="panel-body p20 pb10">
<div id="fixture-parent" onChange={this.handleChange.bind(this)}>
{this.props.children}
</div>
</div>
<div className="section-divider mb40" id="spy1"> </div>
<button className="btn btn-primary tm-tag" onClick={this.props.addMatch}>Add Match</button>
<button className="btn btn-alert tm-tag" onClick={this.props.save}>Save</button>
</div>
</div>
</div>
</div>
);
}

}

export default Fixtures;

最后是我的 child 个人表单组件。

SingleMatchComponent.js

class SingleMatchForm extends Component {

constructor() {
super();
this.state = {
startDate: moment()
};
}

handleChange(date) {
this.setState({
startDate: date
});
}

render() {
return (
<div className="row">
<div key={this.props.id} className="form-group">
<label className="control-label col-md-2">New Match</label>
<div className="col-md-6">
<DatePicker
selected={this.state.startDate}
onChange={this.handleChange.bind(this)}/>
<div className="section-divider mb40" id="spy1"> </div>
</div>
</div>
</div>
);
}

}

export default SingleMatchForm;

最佳答案

在父组件中定义一个函数并将其发送给子组件,使用 props 在子组件中调用父函数。像这样的东西:

父级

class ParentFixturesComponent extends Component {

// don't forget to bind the function in parent
constructor() {
super();
this.state = {
numChildren: 0,
};
this.someFunctionHere = this.someFunctionHere.bind(this);
}

someFunctionHere(param) {
// do whatever you need
}

render() {
const children = [];
for (let i = 0; i < this.state.numChildren; i += 1) {
children.push(<SingleMatchForm key={uuid()}/>)
}

return (
<Fixtures
addMatch={this.onAddMatch.bind(this)}
save={this.onSubmit.bind(this)}
someFunctionHere={this.someFunctionHere}
>
{children}
</Fixtures>
);
}
}

export default ParentFixturesComponent;

child

class Fixtures extends Component {

// constructor and other stuff...

childFunctionHere() {
this.props.someFunctionHere(params);
}

render() {
return (
<div id="fixture-parent" onChange={this.childFunctionHere}>
);
}
}

export default Fixtures;

关于javascript - 在 React 中将数据从子级传递给父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43953618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com