如何将React Native父组件调用子组件方法改写成长尾?
- 内容介绍
- 相关推荐
本文共计625个文字,预计阅读时间需要3分钟。
React Native 中,使用父组件函数式组件调用子组件的类组件方法,可以通过引用子组件的实例来实现。以下是一个简化的示例:
javascriptimport React, { Component } from 'react';import { Text, View, TouchableOpacity } from 'react-native';
class ChildComponent extends Component { // 构造函数中接收ref constructor(props) { super(props); this.childRef=React.createRef(); }
// 子组件方法 childMethod=()=> { console.log('Child method called'); }
render() { return ( Child Component Click me ); }}
class ParentComponent extends Component { // 父组件方法 parentMethod=()=> { // 通过ref调用子组件方法 if (this.childRef.current) { this.childRef.current.childMethod(); } }
render() { return ( Parent Component Call Child Method ); }}
export default ParentComponent;
react-native 父函数组件调用类子组件的方法,代码如下所示:
import React, {Component} from 'react'; import {Text, View, TouchableOpacity} from 'react-native'; // 父 let child onRefbbb = (ref) => { child = ref } clickccc = () => { child.myName() } const Parent =()=> { return( <View> <Child onRefaaa={onRefbbb} /> <TouchableOpacity onPress={()=>clickccc()}> <Text>onClick</Text> </TouchableOpacity> </View> ) } export default Parent // 子 class Child extends Component { componentDidMount(){ this.props.onRefaaa(this) } myName = () =>{ alert(11111111111111) } render() { return (<View></View>) } }
补充:下面看下React 函数式组件之父组件调用子组件的方法
前言:
最近做一个React项目,所有组件都使用了函数式组件,遇到一个父组件调用子组件方法的问题,让我好一阵头疼。
我们都知道,React 中子组件调用父组件方法,使用 props 传递过来就可以在子组件中使用。但是父组件如何调用子组件方法呢?请看下面代码:
第一步:
在父组件中,使用 useRef 创建一个 ref
import { useRef } from 'react' // 父组件中代码 const FCom = () => { const cRef = useRef(null); return ( <div> <ChildCom ref={cRef} /> </div> ) }
第二步:
子组件中代码:(使用了 forwardRef,useImperativeHandle)
import { forwardRef, useImperativeHandle } from 'react' const ChildCom = forwardRef(({ // 这里是一些props参数 }, ref) => { useImperativeHandle(ref, () => ({ getData: getData, otherFun: otherFun })) function getData() { // to do something } function otherFun() { console.log('这是其他方法') } return ( <div>子组件</div> ) })
第三步:
此时,在父组件中就可以随心所欲的调用子组件中的方法了!
import { useRef } from 'react' // 修改父组件中代码 const FCom = () => { const cRef = useRef(null); const handleClick = () => { cRef.current && cRef.current.getData() } return ( <div> <ChildCom ref={cRef} /> </div> ) }
到此这篇关于react-native父函数组件调用类子组件的方法的文章就介绍到这了,更多相关react-native父函数组件调用类子组件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计625个文字,预计阅读时间需要3分钟。
React Native 中,使用父组件函数式组件调用子组件的类组件方法,可以通过引用子组件的实例来实现。以下是一个简化的示例:
javascriptimport React, { Component } from 'react';import { Text, View, TouchableOpacity } from 'react-native';
class ChildComponent extends Component { // 构造函数中接收ref constructor(props) { super(props); this.childRef=React.createRef(); }
// 子组件方法 childMethod=()=> { console.log('Child method called'); }
render() { return ( Child Component Click me ); }}
class ParentComponent extends Component { // 父组件方法 parentMethod=()=> { // 通过ref调用子组件方法 if (this.childRef.current) { this.childRef.current.childMethod(); } }
render() { return ( Parent Component Call Child Method ); }}
export default ParentComponent;
react-native 父函数组件调用类子组件的方法,代码如下所示:
import React, {Component} from 'react'; import {Text, View, TouchableOpacity} from 'react-native'; // 父 let child onRefbbb = (ref) => { child = ref } clickccc = () => { child.myName() } const Parent =()=> { return( <View> <Child onRefaaa={onRefbbb} /> <TouchableOpacity onPress={()=>clickccc()}> <Text>onClick</Text> </TouchableOpacity> </View> ) } export default Parent // 子 class Child extends Component { componentDidMount(){ this.props.onRefaaa(this) } myName = () =>{ alert(11111111111111) } render() { return (<View></View>) } }
补充:下面看下React 函数式组件之父组件调用子组件的方法
前言:
最近做一个React项目,所有组件都使用了函数式组件,遇到一个父组件调用子组件方法的问题,让我好一阵头疼。
我们都知道,React 中子组件调用父组件方法,使用 props 传递过来就可以在子组件中使用。但是父组件如何调用子组件方法呢?请看下面代码:
第一步:
在父组件中,使用 useRef 创建一个 ref
import { useRef } from 'react' // 父组件中代码 const FCom = () => { const cRef = useRef(null); return ( <div> <ChildCom ref={cRef} /> </div> ) }
第二步:
子组件中代码:(使用了 forwardRef,useImperativeHandle)
import { forwardRef, useImperativeHandle } from 'react' const ChildCom = forwardRef(({ // 这里是一些props参数 }, ref) => { useImperativeHandle(ref, () => ({ getData: getData, otherFun: otherFun })) function getData() { // to do something } function otherFun() { console.log('这是其他方法') } return ( <div>子组件</div> ) })
第三步:
此时,在父组件中就可以随心所欲的调用子组件中的方法了!
import { useRef } from 'react' // 修改父组件中代码 const FCom = () => { const cRef = useRef(null); const handleClick = () => { cRef.current && cRef.current.getData() } return ( <div> <ChildCom ref={cRef} /> </div> ) }
到此这篇关于react-native父函数组件调用类子组件的方法的文章就介绍到这了,更多相关react-native父函数组件调用类子组件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

