最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - call child function from parent in reactjs - Stack Overflow

programmeradmin0浏览0评论

My parent ponent

import EditReview from './partials/editReview'

class VenueDetails extends Component {
  constructor(props) {
    super(props)
    this.child = React.createRef();
  }
  render() {
    return (
      <div className="place-review-text">
        <EditReview {...this.props}/>
      </div>
    )
  }
}

My child ponent

class EditReview extends Component {
  onEditClick(review, editIndex) {
    console.log('ppp')
  }

  render() {
    const { handleSubmit, user, pristine, index, mentCrossClick } = this.props
    return (
      <div>
        <Field
          name="content"
          ponent={renderTextArea}
          className="form-control"
          label="Write your review..."
          rows={2}
        />
      </div>
    )
  }
}

export default EditReview

I need to call onEditClick from the parent ponent. I tried this but doesn't work.

Kindly help me

Edit

After upgrade I am getting this

Error in ./~/react-dom/lib/ReactServerRendering.js
Module not found: 'react/lib/React' in /home/user/ashish/LTC/lovethesecities-frontend/node_modules/react-dom/lib

After resolving all the errors call child function from parent in react 16

My parent ponent

import EditReview from './partials/editReview'

class VenueDetails extends Component {
  constructor(props) {
    super(props)
    this.child = React.createRef();
  }
  render() {
    return (
      <div className="place-review-text">
        <EditReview {...this.props}/>
      </div>
    )
  }
}

My child ponent

class EditReview extends Component {
  onEditClick(review, editIndex) {
    console.log('ppp')
  }

  render() {
    const { handleSubmit, user, pristine, index, mentCrossClick } = this.props
    return (
      <div>
        <Field
          name="content"
          ponent={renderTextArea}
          className="form-control"
          label="Write your review..."
          rows={2}
        />
      </div>
    )
  }
}

export default EditReview

I need to call onEditClick from the parent ponent. I tried this but doesn't work.

Kindly help me

Edit

After upgrade I am getting this

Error in ./~/react-dom/lib/ReactServerRendering.js
Module not found: 'react/lib/React' in /home/user/ashish/LTC/lovethesecities-frontend/node_modules/react-dom/lib

After resolving all the errors call child function from parent in react 16

Share Improve this question edited Jan 21, 2020 at 13:28 Ashh asked Sep 1, 2018 at 6:13 AshhAshh 46.5k16 gold badges111 silver badges137 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

React docs have a example of this using refs

https://reactjs/docs/refs-and-the-dom.html

I’m also wondering the use case of wanting to do this, maybe some context could help with an answer?

Try doing it like this:

import EditReview from './partials/editReview'

class VenueDetails extends Component {
  render() {
    return (
      <div className="place-review-text">
        <EditReview ref={Ref => this.child=Ref } {...this.props} />
      </div>
    )
  }
}

and call the function in parent ponent as this.child.onEditClick(param1,param2)

EDIT1:

if you have to do it with react 15.x itself what you can do it is create the function in parent and pass it as a prop to child

发布评论

评论列表(0)

  1. 暂无评论