1. export interface RChild{
    2. input:()=>void
    3. ...
    4. }
    5. export interface TChild{
    6. setIndex:(key)=>void
    7. }
    8. const mapStateToProps=({nav})=>({index:nav.index})
    9. const mapDispathToProps={
    10. setIndex:(key)=>({
    11. type:'nav/setIndex',
    12. payload:key
    13. })
    14. }
    15. const child=forwardRef<RChild,TChild>({setIndex},ref}=>{
    16. useImperativeHandle(ref,()=>({
    17. input, //传给父类的方法或者值
    18. })
    19. return (<>.....</>)
    20. })
    21. export defualt child
    22. //注意
    23. //如果child组件需要是基于connect的话,父类可以获取不到子类方法,界面也可能警告attempts to access this ref
    24. export defualt connect(mapStateToProps,mapDispathToProps,null,{forwardRef:true})(child)
    1. const Parent =()=>{
    2. const preRef=useRef<RChild>(null)
    3. // 调用input方法时;
    4. //preRef.current!.input()
    5. return (<>
    6. <Child ref={preRef}/>
    7. </>)
    8. }
    9. export defualt Parent