react-native-material-textfield icon indicating copy to clipboard operation
react-native-material-textfield copied to clipboard

Change textfield content

Open Flow-Flinc opened this issue 5 years ago • 6 comments

I want to update the textfield content from outside, so I give in a new value into field value. But the component don't accept the new content.

I forked the code and removed the state of the field value and ask if this is a feature you want to have on your main branch?

https://github.com/Flow-Flinc/react-native-material-textfield

Flow-Flinc avatar Nov 29 '19 14:11 Flow-Flinc

I just faced this issue, I think this can be resolved by using a .setValue method via ref as mentioned in docs

karanpratapsingh avatar Dec 03 '19 15:12 karanpratapsingh

value is not getting set when i pass prop, value={this.state.customerList}

Edit: Able to set through

  1. ref={ref => { this.customerName = ref; }}
  2. this.customerName.setValue( );

ajinkyadesai-git avatar Dec 20 '19 07:12 ajinkyadesai-git

Hi, I am seeing the same problem. Updating a bound state variable does not rerender the text contents. I don't think it should be necessary to create a ref for each and every textfield used.

vandero avatar Apr 01 '20 10:04 vandero

@vandero I agree...what might work for you is to wrap this component so that you can call setValue when the component updates:

import React, {
  Component
} from 'react';

import {
  TextField,
} from 'react-native-material-textfield';

export class MyTextField extends Component {
  constructor(props) {
    super(props);
  }

  componentDidUpdate(prevProps) {
    if ( this.props.value !== prevProps.value ) {
      this.textFieldRef.setValue(this.props.value);
    }
  }

  render() {
    return (
      <TextField
        {...this.props}
        ref={reference => { this.textFieldRef = reference }}
      />
    );
  }
}

hmolotsi avatar Apr 18 '20 15:04 hmolotsi

It happens because getDerivedStateFromProps prevents it from updating.

I opened a pull request to address this issue.

https://github.com/n4kz/react-native-material-textfield/pull/278

nadav2051 avatar Jun 18 '20 11:06 nadav2051

value is not getting set when i pass prop, value={this.state.customerList}

Edit: Able to set through

  1. ref={ref => { this.customerName = ref; }}
  2. this.customerName.setValue( );

For functional component: <OutlinedTextField ref={ref => ref?.setValue(nameValue || '') } />

sagormax avatar Nov 16 '23 04:11 sagormax