我在父组件中有一些操作按钮。单击此类按钮之一时,我想触发子组件中的一个功能。目前,我正在尝试使用 useRef 钩子来实现它。但该解决方案似乎很乏味,并且还给了我警告:
我当前的代码如下所示:
import React, {useContext, useEffect, useState, useRef} from 'react';
const ParentComponent = ({...props})=> {
const myRef = useRef();
const onClickFunction = () => {
if(myRef.current) {
myRef.current.childFunction();
}
}
return (
<ChildComponent ref = {myRef}/>
);
}
Run Code Online (Sandbox Code Playgroud)
子组件
const ChildComponent = (({}, ref,{ actionButtons, ...props}) => {
const [childDataApi, setChildDataApi] = useState(null);
const childFunction = () => {
//update childDataApi and pass it to parent
console.log("inside refreshEntireGrid");
}
});
Run Code Online (Sandbox Code Playgroud)
首先,是否有比尝试从父级触发 childFunction 更好的解决方案?为此,我遵循以下解决方案: Can't access child function fromparent …
我将Mac OS升级到了Sierra.升级后,当我尝试时,npm install我收到以下错误:
ECMDERR无法执行"git ls-remote --tags --heads https://github.com/angular/bower-angular-animate.git ",退出代码#128致命:无法访问' https:// github .com/angular/bower-angular-animate.git / ':SSL:CA证书集,但禁用证书验证
我写了一个剧本
npm安装
还包括
凉亭安装
我尝试了几种可能的解决方案来修复错误,但似乎没有任何效果.
任何帮助,将不胜感激!