例如,我想在消息组件中获取引用: 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函数。谢谢