函数式组件-父组件调用子组件方法

import React, { useState , useImperativeHandle , forwardRef  } from 'react';
const Index: React.FC = (props ,ref ) => {
  const [hasIdCard, setHasIdCard] = useState('1');

  useImperativeHandle(ref, () => ({
    setHasIdCard: (hasIdCard) => {
      setHasIdCard(hasIdCard);
    }
  }));

  return (
    <>
  );
};

export default forwardRef(Index);
import React, {  useRef , useEffect} from 'react';
import Child from './components/child/index'
const Index: React.FC<{}> = () => {
  const ref = useRef();
  useEffect(() => {
    ref.current.setHasIdCard('1')
  }, []);
  
  return 
}

你可能感兴趣的:(函数式组件-父组件调用子组件方法)