难道不接受从自定义组件获取引用吗?

时间:2019-10-24 02:27:43

标签: react-native react-hooks

例如,我想在消息组件中获取引用: Main.js:

import * as React from "react"
export const Main: React.FunctionComponent<Props> = observer((props) => {
  const ref = React.useRef()

  React.useEffect(() => {
    ///Can not get ref from message
    ref.current.gotoPosition(5)
  }, [])
  return (
    <View style={styles.container}>
      <Message
        ref={ref}
        />
    </View>
  )
}

Message.js:

import * as React from "react"
// eslint-disable-next-line react/display-name
export const Message = React.forwardRef((props, ref) => ({
  const { ... } = props

  const gotoPosition = (index) => {
    console.log('in here')
  }
  return (
    <View>
....
</View>
  )
}
)

即使我使用React.forwardRef,也无法从Message获取引用。如何通过ref.gotoPosition(5)之类的ref访问Message中的gotoPosition函数。谢谢

0 个答案:

没有答案