当业务场景中出现父组件中,遍历生成多个子组件,又需要调用子组件的方法。则按以下方式进行编码。
//子组件不能用ref属性绑定,无法生效,得用新命名的ref,例如:onRef
父组件:
//父组件关键代码
const child = useRef<any>([]);
<ChildCom onRef={f => { child.current[index] = f; }}/>
//调用子组件的方法
child.current[index].onChangeAll(value);
子组件
useImperativeHandle(onRef, () => (
{
onChangeAll: async value => { console.log(value)},
}));