Home > Back-end >  Show search results from api call - only showing in Console - React
Show search results from api call - only showing in Console - React

Time:10-06

I'm new in JavaScript/React and I have a problem when trying to render the results of my search from an API. It only shows in the console, but not in the HTML instead.

import React from 'react';

class TestJS extends React.Component {
  constructor(props) {
      super(props);
      this.state={
          value:'',
          value2:'',
          resultFound : true,
          filteredData : ""
      };

      this.handleChange=this.handleChange.bind(this);
      this.domainChange=this.domainChange.bind(this);
      this.search=this.search.bind(this);
      this.multiFilter=this.multiFilter.bind(this); 
  }
   
  
    multiFilter(filters) {
      const filterKeys = Object.keys(filters);
     // filters all elements passing the criteria
      return Object.values((item) => {
          // dynamically validate all filter criteria
          return filterKeys.every(key => !!~filters[key].indexOf(item[key]));
      });
  }

  handleChange(event){
      this.setState({value:event.target.value});
  }

  domainChange(event) {
      this.setState({value2:event.target.value});
  }
  
  search(){
      let _this = this;
      let {value, value2 } =this.state;
      const url=`https://exampleapi.com/search=?$user{value2}&domain=${value}`;

      fetch(url,{
          method:'GET'
      })
     
      .then(response => response.json())
          .then(function(data){
              console.table(data, value, value2);
              let filters = {
                  "domain" : [value],
                  "user" : [value2]
              };
              let  filtered = _this.multiFilter(data, filters);
              console.table(filtered);
              
              if(filtered.length <= 0){
                _this.setState({resultFound : false})
              }
              else{
                _this.setState({filteredData : filtered, resultFound : true})
              }
              
          });
          
  }
  
 
   render() {
      return (
          <div>
              <form >
                  <div className="row">
                      <div className="col-md-4">
                          <select value={this.state.value} onChange={this.handleChange} className="form-control" placeholder="Enter user">
                              <option selected>Choose...</option>
                              <option value="tree">TREE</option>
                              <option value="forest">FOREST</option>
                              <option value="apple">APPLE</option>
                              <option value="ground">GROUND</option>
                              <option value="flowers">FLOWERS</option>
                              <option value="grass">GRASS</option>
                          </select>
                      </div>
                      <div className="col-md-4">
                          <div className="form-group is-empty">
                              <input placeholder="Enter user" className="form-control" value={this.state.value2} onChange={this.domainChange}/>
                              <span className="material-input"></span></div>
                      </div>
                      <div className="col-md-4">
                          <button type="button"
                                  className="btn btn-primary btn-block"
                                  onClick={()=> this.search()}
                          >Search</button>
                      </div>
                      {!this.state.resultFound ? <div> No Data Found!!</div> : 
                        JSON.stringify(this.state.filteredData)}
                  </div>
              </form>
          </div>
          
      )
      
  }
}

export default TestJS; 

I know this may be super simple to solve, but I've spent a lot of hours trying to figure it out what is wrong in my code, and being a newbie without guidance, this is hard to achieve. Thanks in advance

CodePudding user response:

I have some suggestions to the code:

1- FilteredData is suposed to be an array? If so, you should initialize the state as an empty array

2- Result found may be not necessary as you can achieve the same if filteredData.length === 0 and you are not using loadings

3- Im note sure about JSON.stringify use there

CodePudding user response:

I noticed that in your state you initialized filteredData as a string, but by looking at your code it seems to be an object.

Instead of initializing it like this

filteredData : ""

try changing it to this

filteredData : {}
  • Related