reactjsreact-nativeuniquedatasourcereact-native-listview

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


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: '',     
    }));
  };

Solution

  • 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: '',     
      }));
      )
    };