Skip to content

Instantly share code, notes, and snippets.

@cheeaun
Created April 24, 2020 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheeaun/35c96ca893db79c9bef8b7b89ea131b7 to your computer and use it in GitHub Desktop.
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
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