我的父级组件
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>
)
}
}
我的子组件
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
我需要从父组件调用onEditClick
。我尝试了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
答案 0 :(得分:1)
尝试这样做:
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>
)
}
}
并以this.child.onEditClick(param1,param2)
的形式调用父组件中的函数
EDIT1 :
如果必须使用react 15.x本身来做,可以做的是在父级中创建函数并将其作为道具传递给子级
答案 1 :(得分:0)