Reactjs React/React Native:如何在函数组件中使用回调引用?
在此库中: 从自述文件中引用的这两行代码(文本输入组件的自动滚动部分)中,父组件可以使用回调ref技术获取子组件的ref数组:Reactjs React/React Native:如何在函数组件中使用回调引用?,reactjs,react-native,react-ref,Reactjs,React Native,React Ref,在此库中: 从自述文件中引用的这两行代码(文本输入组件的自动滚动部分)中,父组件可以使用回调ref技术获取子组件的ref数组: <KeyboardAwareScrollView style={styles.container} getTextInputRefs={() => { return [this._textInputRef];}} > <TextInput style={styles.textInput}
<KeyboardAwareScrollView
style={styles.container}
getTextInputRefs={() => { return [this._textInputRef];}}
>
<TextInput
style={styles.textInput}
placeholder={'My Input'}
ref={(r) => { this._textInputRef = r; }}
/>
</KeyboardAwareScrollView>
{return[this._textInputRef];}
>
{this._textInputRef=r;}}
/>
GetTestInputRefs是一个回调函数,您可以在其中返回对放置在scrollView中的子TextInput组件的引用数组
然而,据我所知,在功能组件中没有类似的东西。当父scrollview和子输入都是功能组件时,如何执行相同的操作
没有必要将此作为示例,但使用它会非常好
欢迎提供任何概念证明。以下是在功能组件中使用REF的示例
const MyComponent = () => {
const textRef = React.useRef();
return (
<KeyboardAwareScrollView getTextInpurRefs={() => [textRef]}>
<TextInput ref={textRef} />
</KeyboardAwareScrollView>
)
}
constmycomponent=()=>{
const textRef=React.useRef();
返回(
[textRef]}>
)
}
以下是在功能组件中使用REF的示例
const MyComponent = () => {
const textRef = React.useRef();
return (
<KeyboardAwareScrollView getTextInpurRefs={() => [textRef]}>
<TextInput ref={textRef} />
</KeyboardAwareScrollView>
)
}
constmycomponent=()=>{
const textRef=React.useRef();
返回(
[textRef]}>
)
}
您可以使用
例如:
export default () => {
const textInputRef = React.useRef(null);
return (
<KeyboardAwareScrollView
style={styles.container}
getTextInputRefs={() => { return [textInputRef];}}
>
<TextInput
style={styles.textInput}
placeholder={'My Input'}
ref={textInputRef}
/>
</KeyboardAwareScrollView>
);
}
导出默认值()=>{
const textInputRef=React.useRef(null);
返回(
{return[textInputRef];}
>
);
}
您可以使用
例如:
export default () => {
const textInputRef = React.useRef(null);
return (
<KeyboardAwareScrollView
style={styles.container}
getTextInputRefs={() => { return [textInputRef];}}
>
<TextInput
style={styles.textInput}
placeholder={'My Input'}
ref={textInputRef}
/>
</KeyboardAwareScrollView>
);
}
导出默认值()=>{
const textInputRef=React.useRef(null);
返回(
{return[textInputRef];}
>
);
}