Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何添加方法来反应功能组件_Reactjs_Typescript_React Functional Component - Fatal编程技术网

Reactjs 如何添加方法来反应功能组件

Reactjs 如何添加方法来反应功能组件,reactjs,typescript,react-functional-component,Reactjs,Typescript,React Functional Component,我有一个组件,其代码如下: const Abc: React.FC<AbcTypes> = ({...}) => {...} Abc.getLayout = () => {...} const Abc:React.FC=({…})=>{…} Abc.getLayout=()=>{…} 我不清楚如何在Typescript中的Abc组件上定义/扩展方法getLayout?如果您询问如何键入,则可以执行以下操作: const Abc: React.FC<AbcTyp

我有一个组件,其代码如下:

const Abc: React.FC<AbcTypes> = ({...}) => {...}

Abc.getLayout = () => {...}
const Abc:React.FC=({…})=>{…}
Abc.getLayout=()=>{…}

我不清楚如何在Typescript中的Abc组件上定义/扩展方法
getLayout

如果您询问如何键入,则可以执行以下操作:

const Abc: React.FC<AbcTypes> & { getLayout: () => SomeType } = ({...}) => {...}

Abc.getLayout = () => {...}

坏主意。请使用
useState
useffect
代替。谢谢,这样做了。
type AbcRef = {
  getLayout: () => string[]
}
const Abc = forwardRef<AbcRef>((props, ref) => {
  useImperativeHandle(ref, () => ({
    getLayout: () => ['abc'],
  }))

  return <div>Abc</div>
})

const Parent: FC = () => {
  const abcRef = useRef<AbcRef>(null)

  useEffect(() => {
    console.log(abcRef.current?.getLayout())
  }, [])

  return <Abc ref={abcRef} />
}