Created
April 24, 2020 13:46
-
-
Save cheeaun/35c96ca893db79c9bef8b7b89ea131b7 to your computer and use it in GitHub Desktop.
React Native's Text component, but imperative instead for setting the text value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { forwardRef, useState, useImperativeHandle } from 'react'; | |
import { Text } from 'react-native'; | |
export default forwardRef((props, ref) => { | |
const [text, setText] = useState(''); | |
useImperativeHandle(ref, () => ({ | |
setText: t => { | |
if (t) setText(t); | |
}, | |
})); | |
return <Text {...props}>{text}</Text>; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment