1

I don't know whether it's a bug on react native TextInput or if I am doing something wrong. When I try to show initial value based on state as default value on TextInput it works only for string and not for number.

        this.state = {referralCode: 10}
<TextInput value={this.state.referralCode}  />

This due to some reason does not works and shows empty but it should read the intezer value from the state. As soon as I update the state with some string as initial value the TextInput shows it on initial render.

               this.state = {referralCode: 'Rishav'};

 <TextInput value={this.state.referralCode}

/>

It happens to render the initial state value now because it is in string.

Also, <TextInput value={10}/> fails but <TextInput value={'10'}/> works

1 Answer 1

1

According to documentation . Value props need string . if you want to use number set in state , you can do like this

constructor(props) {
    super(props);
    this.state = {
      referralCode: 10
    };
  }

<TextInput
            onChangeText={referralCode => this.setState(referralCode)}
            value={`${this.state.referralCode}`}
            blurOnSubmit={true}
            autoCapitalize="none"
            returnKeyType="done"
          />


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

1 Comment

I laugh at myself for this silly mistake. Thankyou : )

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.