react-native - 在 Parent 中传递两个函数
问题描述
我在将两个函数从子级传递给父级时遇到问题。子函数是 =>
setValueLocally = () => {...}
show_User_Poll = () => {...}
在 Parent 中,我通过创建另外两个函数来访问这些函数。
savePollData() { this.refs.savePoll.setValueLocally() }
_show_User_Poll() { this.refs.showPoll.show_User_Poll() }
我正在父级中呈现这些功能。问题是如何为上述两个功能设置参考?
<Child ref = {"savePoll", "showPoll"} />
我正在这样做,但它给出了错误..
对于单一功能,它对我有用
<Child ref="savePoll"/>
解决方案
您应该在子组件上创建一个引用,然后您可以从 Parent 访问子组件的所有方法。PS:您只能引用类组件,因此您的 Child 需要是一个类。
const Parent = props => {
const childRef = React.useRef(null);
const savePollData = () => {
childRef.current.savePollData();
};
const _show_User_Poll = () => {
childRef.current.show_User_Poll();
};
return <Child ref={childRef} />;
};
class Child extends React.Component {
setValueLocally = () => {
return null;
};
show_User_Poll = () => {
return null;
};
render() {
return null;
}
}
推荐阅读
- azure-iot-hub - 如何获取 IoTHub 设备的有效 lastActivityTime 和 cloudToDeviceMessageCount 值?
- python - 从谷歌驱动器导入谷歌 Colab 中的模块 - python
- python - 如何使用 python 在 localhost 上托管 HTML 文件?
- python - 划分两个 Pandas Pivoted Tables 并生成第三个表,其中两个表之间的划分结果
- c - 为什么链接器链接了错误的函数?
- java - com.networknt json-schema-validator 不检查 json 中的未知关键字
- flask - HEROKU FLASK“没有 Web 进程正在运行”错误
- c++ - 如何通过指针传递 lambda 函数?
- java - 查询具有@ManyToMany 关系的表(休眠)
- powershell - Powershell 7 - 并行 While 循环