本文介绍了在reactjs中从父级调用子函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的父组件

从'./partials/editReview'导入EditReview类场地细节扩展组件{构造函数(道具){超级(道具)this.child = React.createRef();}使成为() {返回 (<div className="place-review-text"><EditReview {...this.props}/>

)}}

我的子组件

class EditReview 扩展组件 {onEditClick(review, editIndex) {console.log('ppp')}使成为() {const { handleSubmit, user, pristine, index, commentCrossClick } = this.props返回 (<div><字段名称=内容"组件={renderTextArea}className="表单控件"label="写你的评论..."行={2}/>

)}}导出默认 EditReview

我需要从父组件调用 onEditClick.我尝试了这个,但没有用.>

请帮助我

编辑

升级后我得到这个

./~/react-dom/lib/ReactServerRendering.js 中的错误未找到模块:/home/user/ashish/LTC/lovethesecities-frontend/node_modules/react-dom/lib 中的react/lib/React"

解决所有错误后从父级调用子函数在反应 16

解决方案

React 文档有一个使用 refs 的例子

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

我也想知道想要这样做的用例,也许一些上下文可以帮助回答?

My parent component

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 component

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

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

export default EditReview

I need to call onEditClick from the parent component. 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

解决方案

React docs have a example of this using refs

https://reactjs.org/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?

这篇关于在reactjs中从父级调用子函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 16:31