0

I want to filter content based on the input typed, I don't know where did I go wrong. Whenever I typed my input it just doesn't filter the content instead of that it is showing all the content there.

  function searchingFor(searchingTerm) {
        return function(x){
            console.log("searching",x);
            return x.name.toLowerCase().includes(searchingTerm.toLowerCase())|| searchingTerm;
        }
    }

    class Main extends React.Component{

        componentWillMount(){
            this.props.fetchTopicsTableContent(this.state.sortBy,'ASC',0,this.props.match.params.Occupation).then(result=> (this.setState({rows:result.payload.data})))
            this.props.countTableContent(this.props.match.params.Occupation).then(result=>(this.setState({count:result.payload})));
        }

        constructor(props){
            super(props);

            this.state={
                searchTerm:"",
                rows:""
            }
        }

    searchHandler(e){
            this.setState({searchTerm:e.target.value})
            // console.log("rows",this.state.rows)
            {this.state.rows.filter(searchingFor(this.state.searchTerm)).map(item=>{
                console.log(item);
                 // this.setState({rows:item})
            })}
        }

    render(){
        return(
            <form>
               <input type="text"
                  value={this.state.searchTerm}
                  onChange={this.searchHandler.bind(this)}
                />
            </form>);}}
2
  • why are you running map? and are you sure searchingFor() returns true results ? Commented Sep 21, 2018 at 12:36
  • just use parameter directly filter(searchingFor(e.target.value) Commented Sep 21, 2018 at 12:59

3 Answers 3

1

this.setState({searchTerm:e.target.value}) #setState is async operation and if you want to complete action how this operations will finish.

You should use callback function as for the 2d argument: this.setState({searchTerm:e.target.value}, () => {...here is your code})

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

2 Comments

Yes you are correct, when I combine this and the comment which has been verified, this works. Thanks for the comment
When I try to use setState under this, error is showing up. Like I want the item there to store it in a state called rows. If you look up there you can see the comment inside the filter function. It is not working.
1

The JavaScript operator || is defined to return the left value if it evaluates to a truthy value, otherwise the right value instead of returning true itself. So try replacing this

return x.name.toLowerCase().includes(searchingTerm.toLowerCase())|| searchingTerm;

with

return x.name.toLowerCase().includes(searchingTerm.toLowerCase())|| false;

Comments

0

your searchingFor() function is wrong. Make the provided changes:

this.state.rows
.filter(item => item.name.toLowerCase().indexOf(searchingTerm.toLowerCase()) > -1)
.foreach(item => console.log(item));

You can use the spread operator to save the data

Comments

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.