自己封裝的組件,想使用ref,調(diào)用內(nèi)部的方法
1.使用React.forwardRef突梦,包裹組件。
export default React.memo(React.forwardRef(QuestionComments));
2.接收ref屬性羽利。
image.png
3.封裝外部需要調(diào)用的方法宫患。
useImperativeHandle(ref, () => ({
save: () => {
commentsForm.submit();
},
}), [commentsForm]);