React-native: How to copy text from <Text> component

Created on 26 Aug 2015  路  20Comments  路  Source: facebook/react-native

Hi, guys. How to copy text from component? Any idea?

Locked

Most helpful comment

For most people, this is enough:

<Text selectable>TEXT</Text>

All 20 comments

Use onChangeText

image

And then? I want to copy text from component.

onChangeText: Callback that is called when the text input's text changes. Changed text is passed as an argument to the callback handler.

onChangeText={(text) => this.setState({text}) }

https://facebook.github.io/react-native/docs/textinput.html#onchangetext

But I want to copy text to clipboard. Sorry for my bad english.

My English is not good. I speak Portuguese. 馃槄

Try using it in the callback:
https://www.npmjs.com/package/react-native-clipboard

Cool, it works! But maybe you know react-native component with tooltip?
Like this:
img_0198

I've never seen 馃槙
The Facebook app Groups, made with react-native also does not have it, it displays an ugly popup.

Well, react-native, challenge accepted!

In fact, the popup is to select Text elements. For the TextInput, the selection usually works here.

image

Yep, but only in TextInput.

if you need custom actions with custom tooltip: https://github.com/chirag04/react-native-tooltip

Nice one @chirag04 :)

Is it possible to show the default iOS tooltip with the copy option on a text component? Or do we need to always use a custom tooltip?

@Traviskn You could have a read-only text-input I guess.

has anyone found a solution to this?
On iOS, native behavior is if you hold any text long enough, it should show the tooltip for copying.
I don't know how it works on Android, though.

Any plans for making this work with iOS anytime soon for a normal Text element?

Try it :)
import {Clipboard} from 'react-native';

setClipboardContent = (msg) => {
Clipboard.setString(msg);
};

For most people, this is enough:

<Text selectable>TEXT</Text>

@jfilter, it works perfectly on android, but on iOS - it whows only 'Copy' dialog, without selecting range.
Did you know how enable opportunity to select text before copy ?

selectable doesn't yet work for react-native-macos but there is a workaround: https://github.com/ptmt/react-native-macos/issues/157

Was this page helpful?
0 / 5 - 0 ratings