11

I got the following Component and I want to init TextInput with defaultValue and then when user type update the value of it.

How do I do that?

Here what I've tried - but this way TextInput is always empty on initialization.

class Note extends Component {
    state = {
        text: ""
    };

    render() {
        const {onChange} = this.props;

        return (
                <TextInput
                    onChangeText={(text) => {
                         this.setState({text}); 
                         onChange(text);
                    }
                    value={this.state.text}
                    defaultValue={this.props.text}
                />
        );
    } }

"react": "^16.4.1"
"react-native": "^0.55.4",

6
  • Do you want to append the user input with Default value? Commented Aug 10, 2018 at 7:18
  • You want to update the value on text change . right ? Commented Aug 10, 2018 at 7:19
  • @MohammedAshfaq sorry what do you mean? Commented Aug 10, 2018 at 7:50
  • @anilsidhu Yes. Commented Aug 10, 2018 at 7:50
  • @chenop i am making a answer for make better code formatting Commented Aug 10, 2018 at 8:31

6 Answers 6

23

Finally solved it,

The key is the following - "value" has preceding over "defaultValue" unless "value" is undefined!

So, Init this.state.text = undefined, this way defaultValue can be initialized with this.props.text.

class Note extends Component {
    state = {
        text: undefined
    };

    render() {
        const {onChange} = this.props;

        return (
            <View>
                <TextInput
                    onChangeText={(text) => {
                         this.setState({text}); 
                         onChange(text);
                    }
                    value={this.state.text}
                    defaultValue={this.props.text}
                />
            </View>
        );
    }
}

I found this a bit more clean than @anilSidhu solution.

Sign up to request clarification or add additional context in comments.

2 Comments

definitely best answer
I faced the issue setting integer value as default. value.toString() solved my problem.
3

You can set default value in state itself like following:

class Note extends Component {
    state = {
        text: this.props.text
    };

    render() {
        const {onChange} = this.props;

        return (
                <TextInput
                    onChangeText={(text) => {
                         this.setState({text}); 
                         onChange(text);
                    }
                    value={this.state.text}
                />
        );
    } }

7 Comments

please accept my answer if it become useful to you.
Thanks for answering But I think that's not good enough. This way the props.text will be used only once (when constructor will be invoked) I want the props.text to be used every time it is changed.
Also please note that if Note is encapsulated by another component which is connected to Redux (my case but I think its the common case) and the encapsulated Component init the text attribute which is passed to Note - than this.props.text will be initialized in the first time with undefined.
yes you are right as I think at first time render pass blank value to TextInput's value and then it will take this.props.text. may be this is the reason.
thanks for you time man - The first time this.props.text is undefined. the encapsulated component use mapStateToProps to init text but it init text only after it was pass (as undefined) to Note
|
3
class Note extends Component {
  state = {
    text: undefined
  };
  componentDidMount() {
    this.setState({ text: this.props.text })
  }
  render() {

    return (
      <TextInput
        onChange={(text) => {
          this.setState({ text: text.target.value });

        }}
        value={this.state.text}
      />
    );
  }
}

It should work for you and if you are still facing the issue let me know please

Comments

3

For someone having trouble with this problem. I've experienced it. i expect my number Value to be displayed on Textinput. But placeholders keep showing.

The solution is your Value must be String. So i convert my Number to String use toString() method.

Hope this helps.

2 Comments

Holy crap. 2022 and this is what we're dealing with. Thanks for this.
Thanks, Mate, for this, and people say react native is the best.
2

Basically, when you're having both value and defaultValue props, always value prop will take precedence and thus defaultValue won't be reflected.

Comments

1

Sometimes if you are showing number type to default value it doesn't work. you must convert to string type as follows:

defaultValue(String(this.props.text)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.