root-two / react-native-drawer

React Native Drawer
MIT License
2.54k stars 392 forks source link

how to use ref in function component #386

Open gitdogogo opened 4 years ago

gitdogogo commented 4 years ago

const fn= ({}: Props) => { let _drawer = React.useRef(); function close() { _drawer.current.close(); } function open() { _drawer.current.open(); } return ( <Drawer styles={drawerStyles} ref={(ref) => (_drawer.current = ref)} drawerType="overlay" openDrawerOffset={0.5} acceptTap={true} open={true} content={}

); };

when i used like this , but it's not worked

VladosK0k0s commented 4 years ago

In functional component you need to use hook useRef

mdaniyalaslam commented 3 years ago

use this : ref={_drawer} instead of ref={(ref) => (_drawer.current = ref)}

rajAmukhliS commented 1 year ago

Hope this will help you

import React, {useRef} from 'react';
...
const Index=()=>{
...
const drawerRef=useRef(null);

function close() {
drawerRef.current.close();
}
function open() {
drawerRef.current.open();
}
...
return(
 <Drawer
       ref={drawerRef}
...
>
....
</Drawer>)
}