himelbrand / react-native-numeric-input

a stylish numeric input for react native
MIT License
150 stars 98 forks source link

[BUG] - Value doesn't change when component parent is rendered #70

Closed simo385 closed 3 years ago

simo385 commented 3 years ago

Hi guys,

I have NumericInput component inside another one with the following structure:

Class Product {
    this.state = {
        quantity: 2;
    }
render(){
    return (<View>....</View>
    <NumericInput
       ...
        value={this.state.quantity}
        onChange={...} />)
    }
}

As the Produc is rendered, because of quantity state update, NumericInput doesn't render itself. Inside the NumericInput I see the old quantity value.

I'd like the quantity value is rendered inside NumericInput component as well.

Enviorment (please complete the following information):

Can I solve the bug? Any workaround?

Thanks in advance, Simone

simo385 commented 3 years ago

Hi Everybody,

any updates?

Thanks in advance, Simone

ErkanGorgulu commented 3 years ago

Did you try giving an initial value ? <NumericInput value={selectedItemAmount} onChange={setselectedItemAmount} initValue={selectedItemAmount} />

simo385 commented 3 years ago

Hi @ErkanGorgulu,

yes, I tried, but it doesn't work.

Simone

a777med commented 3 years ago

Hi,

Add this to the NumericInput component (NumericInput.js) and it will work just fine.

componentDidUpdate(prevProps) {
  if (prevProps.value !== this.props.value) {
    this.setState({
      value: this.props.value,
      stringValue: this.props.value.toString(),
    });
  }
}
a777med commented 3 years ago

Also, I think this PR #56 will solve this issue, too.

simo385 commented 3 years ago

Hi,

Add this to the NumericInput component (NumericInput.js) and it will work just fine.

componentDidUpdate(prevProps) {
  if (prevProps.value !== this.props.value) {
    this.setState({
      value: this.props.value,
      stringValue: this.props.value.toString(),
    });
  }
}

Hi @a777med,

I did it, but doesn't work.

Many thanks for your support. Simone

huy-lv commented 1 year ago

@simo385 how did you resolve it

pooryamd commented 1 year ago

Hi,

Add this to the NumericInput component (NumericInput.js) and it will work just fine.

componentDidUpdate(prevProps) {
  if (prevProps.value !== this.props.value) {
    this.setState({
      value: this.props.value,
      stringValue: this.props.value.toString(),
    });
  }
}

This one worked for me like a charm :) My module code:

// this.props refers to the new props
componentDidUpdate(prevProps) {
    const initSent = !(this.props.initValue !== 0 && !this.props.initValue); 
      if (prevProps.value !== this.props.value) {
        this.setState({
          value: this.props.value,
          stringValue: this.props.value.toString(),
        });
      }
    // compare the new value (props.initValue) with the existing/old one (this.state.value)
    if (this.props.initValue !== this.state.value && initSent) {
        this.setState({
            value: this.props.initValue,
            lastValid: this.props.initValue,
            stringValue: this.props.initValue.toString()
        });
    }
}