Search
 
SCRIPT & CODE EXAMPLE
 

CSS

Make changes to api fetch onclick in react

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      isLoading: false,
      results: [],
      error: null,
    };
  }

  fetchData = (url, query, key) => {
    fetch(`the/api/${url}${query}.json?api-key${key}`)
      .then(res => res.ok && res.json())
      .then(data => this.setState({ results: data.results, isLoading: false }))
      .catch(error => this.setState({ error, isLoading: false }));
  }

  componentDidMount() {
    fetchData('api_url', 'all', 'key_here');
  }

  handleCategory = (e) => {
    const cat = e.target.getAttribute('data-facet');
    fetchData('api_url', cat, 'key_here');
  }

  render() {
    const { isLoading, results, error } = this.state;

    if (isLoading) {
      return <div className="loading-icon"></div>
    }

    if (error) {
      return <div className="error"></div>    
    }

    return (
      <div className="wrapper">
        <button data-facet="arts" onClick={this.handleCategory}>Arts</button>
        {results.map(res => ...)}
      </div>
    );
  }
}
Comment

PREVIOUS NEXT
Code Example
Css :: faunadb q.do 
Css :: width cross browser 
Css :: cant change button higeht in @media 
Css :: add filters in drf specifying specific fields 
Css :: gravity form css 
Css :: css change scroll anchor point under navbar 
Css :: gh: stop using --force !!! 
Css :: Responsive testimonial slider HTML CSS 
Css :: css to reduce margin of class container 
Css :: ubuntu tor browser verification failed 
Css :: text decoration too long 
Css :: acrilic css 1 
Css :: à quoi sert clearfix 
Css :: css geight 
Css :: css ditribute width equealy to child components 
Css :: active css not working 
Css :: css keep focus color on div after click 
Css :: background-origen,css 
Css :: which bootstrap css class will you use to put the navbar at the top of the page? feel free to check out the bootstrap website. 
Css :: how to change the theme of a website using css 
Css :: material css navbar 
Css :: why does my css take time to load 
Css :: uytutyu 
Css :: trigger before update 
Css :: p::after p::before css 
Css :: how can i make a menu bar appear by clicking an icon? in css? 
Css :: css conflict 
Css :: input type search in css 
Css :: css garden 
Typescript :: organize imports on save vscode 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =