从react.js中的父类调用子组件函数

时间:2018-01-04 11:22:27

标签: reactjs

在父类中调用save按钮调用方法时遇到一些问题     保存在儿童班。我需要找到一个解决方案来调用保存功能     父组件

class Parent extends React.Component{
 render(){
  return(
   <Child />
   <button onClick={Child.alert()}>save</button>
  **// this button should call alert function now.**
  )
}
}
export default Parent

// child component
class Child extends React.Component{
 alert= () => {
  console.log('child's alert called')
 }
render(){
  return(
   <button onClick={this.alert()}>Save</button>  
  **// this is calling alert right now**
   )
 }
}
export default Child

**//when save button of the parent class called, it should call the alert 
method in the child class.**

0 个答案:

没有答案
相关问题