Enable paste and selection within TextInput - React Native

后端 未结 2 1407
别那么骄傲
别那么骄傲 2021-01-05 21:22

I am trying to implement copy and paste within my TextInput but cant seem to achieve it. I was expecting a tooltip when I long-pressed on my TextInput, however nothing happe

2条回答
  •  死守一世寂寞
    2021-01-05 21:38

    Here is the answer if copy/paste does not work for TextInput - React Native

    Step 1) In Contructor take testWidth property and assign value as '99%'.

    e.g.

    this.state = {testWidth: '99%' };

    Step 2) In componentDidMount change testWidth value like '100%', do it inside of setTimeout.

    e.g.

     setTimeout(() => {
          this.setState({ testWidth: '100%' })
        }, 100)
    

    Step 3) In style attribute of TextInput add dynamic width which we declare in Contractor, e.g

    
    

    Here is the full code: (Just copy and paste in App.js file).

    import React, { Component } from 'react';
        import { TextInput, View } from 'react-native';
    
        export class App extends Component {
          constructor(props) {
            super(props);
            this.state = { text: '', testWidth: '99%' };
          }
          componentDidMount() {
    
            setTimeout(() => {
              this.setState({ testWidth: '100%' })
            }, 100)
          }
          render() {
            return (
              
                 this.setState({ text })}
                  value={this.state.text}
                />
              
            );
          }
        }
    

    Good Luck

提交回复
热议问题