bip-bop
bip-bop

Reputation: 1321

Filter array in React Native

I try to filter array. I need "live filtering" when a user types something in search input in React Native, but I do it like I usually do filter in React. Something goes wrong. Can you take a look and tell me where is a mistake? Now I have error: undefined is not an object (this.state.inputValue.trim) I think that maybe I should do some another way for a filter?

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, Image, TouchableOpacity, TextInput } from 'react-native';
import { TopName, BottomName, TitleLedger } from '../common/text';
import styles from './style';

const rows = [{
  "img": require('../../assets/avatar.png'),
  "name": "Johnny",
  "nick": "@johny",
},{
  "img": require('../../assets/avatar.png'),
  "name": "Johnny",
  "nick": "@johny",
},{
  "img": require('../../assets/avatar.png'),
  "name": "Alex",
  "nick": "@alex",
},{
  "img": require('../../assets/avatar.png'),
  "name": "Johnny",
  "nick": "@johny",
}];

export default class Payment extends Component {
    state={
        inputValue: ''
    }
    onChangeHandler = (e)=> {
         this.setState({inputValue: e.target.value})
         console.log(e.target.value, 'value')
     }
  render() {
    const inputValueLet = this.state.inputValue.trim().toLowerCase();
    let rowsNew = [];
    if(rows.length>0){
            rowsNew = rows.filter(row => {
                return row.name.toLowerCase().match( inputValueLet )
            });
        }
    const { navigator } = this.props;
    const dataRow = rowsNew.map((row, index) => {
            return (
              <View style={styles.content}>
              <Image source={row.img} style={styles.avatar}/>
              <View key={index} id={index} style={styles.operation}>
                <View style={styles.wrapper}>
                  <View style={styles.topName}>
                    <TopName label={row.name} />
                  </View>
                  <View style={styles.bottomName}>
                    <BottomName label={row.nick} />
                  </View>
                </View>
              </View>
              </View>
            )
          })
    return (
        <View>
          <View>
            <TextInput
              style={styles.searchBar}
              type="text"
              value={this.state.inputValue}
              onChange={this.onChangeHandler}
              placeholder='Search'
              />
          </View>
        <View style={styles.container}>
            {dataRow}
        </View>
      </View>
    );
  }
}

Upvotes: 0

Views: 3463

Answers (3)

DianaS pencer
DianaS pencer

Reputation: 1

onChange={(e)=>{setSearch(e.target.value)}}

users.filter(item=>item.name.toLocaleLowerCase().includes(search.toLocaleLowerCase())).map((item)=>

Upvotes: 0

Anurag Singh Bisht
Anurag Singh Bisht

Reputation: 2753

inputValue in your state is declared as string ''.

state = {
     inputValue: ''
}

To resolve error undefined is not an object (this.state.inputValue.trim).

I think you should declare inputValue as object :

state = {
     inputValue: {}
}

Upvotes: 0

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

There is no event.target.value in react native TextInput#onChange. Use event.nativeEvent.text or onChangeText event where you get text as callback argument.

<TextInput
          style={styles.searchBar}
          type="text"
          value={this.state.inputValue}
          onChangeText={inputValue => this.setState({ inputValue })}
          placeholder='Search'
          />

Upvotes: 1

Related Questions