【发布时间】:2020-12-05 15:06:42
【问题描述】:
如何在 ref 中插入属性和函数?像这个例子:
const MyComponent = () => {
const [loading, setLoading] = React.useState(false)
const onTest = () => 'works'
return (
<div {...props}>
)
}
然后我想这样使用属性loading和函数onTest:
const Test = () => {
const myRef = React.useRef()
React.useEffect(() => {
if (myRef.current)
alert('loading is ' + myRef.current.loading + ' function is ' + myRef.current.onTest())
})
return(
<MyComponent ref={myRef} />
)
}
我该怎么做?
【问题讨论】:
标签: javascript reactjs ref