How to check that new input will be unique in react native ListView data-source

68 views Asked by At

I need to check that new input which will be added to the dataSource is not contained in it already.

 _handleSendButtonPress = () => {
    const textArray = this.state.dataSource._dataBlob.s1;
// Here I need to check that this.state.inputValue is in textArray already
    textArray.push(this.state.inputValue);
    this.setState(() => ({
      dataSource: this.state.dataSource.cloneWithRows(textArray),
      inputValue: '',     
    }));
  };
1

There are 1 answers

1
Sergey On BEST ANSWER

If I understand you correctly and textArray is indeed an array, this should work:

_handleSendButtonPress = () => {
  const textArray = this.state.dataSource._dataBlob.s1;
  !textArray.includes(this.state.inputValue) && (
    textArray.push(this.state.inputValue);
    this.setState(() => ({
    dataSource: this.state.dataSource.cloneWithRows(textArray),
    inputValue: '',     
  }));
  )
};