const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
const Container = React.forwardRef((props, ref) => {
return <div ref={ref}>{props.children}</div>
})
const App = () => {
const elRef = React.useRef();
React.useEffect(() => {
console.log(elRef);
elRef.current.style.background = 'lightblue';
}, [elRef])
return (
<Container ref={elRef}/>
);
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View, Button, TouchableOpacity } from 'react-native';
import React from 'react';
export default function App() {
function q()
{
console.log(hello.current.props.title);
}
const hello = React.createRef();
return (
<View style={styles.container}>
<TouchableOpacity onPress={()=>q()}>
<Text>
Invoke Fancy Button
</Text>
</TouchableOpacity>
<FancyButton ref={hello}/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
const FancyButton = React.forwardRef((props, ref) => (
<Button ref={ref} id="myId" title="FancyButton" />
));