如何从子组件调用父组件中的函数
How to call function in parent component from the child component
我的主 Class 中有一个名为 addFunc
的函数。此 class 调用 RenderItem
函数来显示项目列表。每个项目都有一个 onClick
应该执行 addFunc
功能。
我无法从我的 RenderItem
函数中调用 addFunc
函数,因为它们位于不同的组件中。我该如何克服这个问题?
这是我的代码摘要:
const selectedData = []
class Search extends Component {
constructor(props) {
super(props);
this.addFunc = this.addFunc.bind(this);
}
addFunc(resultdata){
console.log(resultdata)
selectedData = [...selectedData, resultdata]
console.log(selectedData)
};
render() {
return (
<ReactiveList
componentId="results"
dataField="_score"
pagination={true}
react={{
and: ["system", "grouping", "unit", "search"]
}}
size={10}
noResults="No results were found..."
renderItem={RenderItem}
/>
);
const RenderItem = (res, addFunc) => {
let { unit, title, system, score, proposed, id } = {
title: "maker_tag_name",
proposed: "proposed_standard_format",
unit: "units",
system: "system",
score: "_score",
id: "_id"
};
const resultdata = {id, title, system, unit, score, proposed}
return (
<Button
shape="circle"
icon={<CheckOutlined />}
style={{ marginRight: "5px" }}
onClick={this.addFunc()}
/>
);
}
您可以将父组件中定义的回调函数作为 prop 传递给子组件
class Parent extends React.Component {
sayHello(name) => {
console.log("Hello " + name)
}
render() {
return <Child1 parentCallback = {this.sayHello}/>
}
}
然后从子组件中调用
class Child1 extends React.Component{
componentDidMount() {
this.props.parentCallback("Foo")
}
render() {
return <span>child component</span>
}
};
你可以用另一个组件包裹 RenderItem
组件然后渲染它,
const Wrapper = cb => {
return (res, triggerClickAnalytics) => (
<RenderItem
res={res}
triggerClickAnalytics={triggerClickAnalytics}
addFunc={cb}
/>
);
};
ReactiveList
的 和 renderItem
将是:renderItem={Wrapper(this.addFunc)}
那么 RenderItem
组件将是
const RenderItem = ({ res, triggerClickAnalytics, addFunc }) => {
...
查看沙盒:https://codesandbox.io/s/autumn-paper-337qz?fontsize=14&hidenavigation=1&theme=dark
我的主 Class 中有一个名为 addFunc
的函数。此 class 调用 RenderItem
函数来显示项目列表。每个项目都有一个 onClick
应该执行 addFunc
功能。
我无法从我的 RenderItem
函数中调用 addFunc
函数,因为它们位于不同的组件中。我该如何克服这个问题?
这是我的代码摘要:
const selectedData = []
class Search extends Component {
constructor(props) {
super(props);
this.addFunc = this.addFunc.bind(this);
}
addFunc(resultdata){
console.log(resultdata)
selectedData = [...selectedData, resultdata]
console.log(selectedData)
};
render() {
return (
<ReactiveList
componentId="results"
dataField="_score"
pagination={true}
react={{
and: ["system", "grouping", "unit", "search"]
}}
size={10}
noResults="No results were found..."
renderItem={RenderItem}
/>
);
const RenderItem = (res, addFunc) => {
let { unit, title, system, score, proposed, id } = {
title: "maker_tag_name",
proposed: "proposed_standard_format",
unit: "units",
system: "system",
score: "_score",
id: "_id"
};
const resultdata = {id, title, system, unit, score, proposed}
return (
<Button
shape="circle"
icon={<CheckOutlined />}
style={{ marginRight: "5px" }}
onClick={this.addFunc()}
/>
);
}
您可以将父组件中定义的回调函数作为 prop 传递给子组件
class Parent extends React.Component {
sayHello(name) => {
console.log("Hello " + name)
}
render() {
return <Child1 parentCallback = {this.sayHello}/>
}
}
然后从子组件中调用
class Child1 extends React.Component{
componentDidMount() {
this.props.parentCallback("Foo")
}
render() {
return <span>child component</span>
}
};
你可以用另一个组件包裹 RenderItem
组件然后渲染它,
const Wrapper = cb => {
return (res, triggerClickAnalytics) => (
<RenderItem
res={res}
triggerClickAnalytics={triggerClickAnalytics}
addFunc={cb}
/>
);
};
ReactiveList
的 和 renderItem
将是:renderItem={Wrapper(this.addFunc)}
那么 RenderItem
组件将是
const RenderItem = ({ res, triggerClickAnalytics, addFunc }) => {
...
查看沙盒:https://codesandbox.io/s/autumn-paper-337qz?fontsize=14&hidenavigation=1&theme=dark