TransWikia.com

React js .bind(this) in hooks

Stack Overflow Asked by Soukaina Benchekroun on November 27, 2021

Hello I want to transform this code My code in link, so that it works with hooks, what I can not do is to transform this.fetchData = this.fetchData.bind(this); which is done in the constructor using hooks, and how to get the state

constructor() {
super();
this.state = {
  data: [],
  pages: null,
  loading: true,
  sorted: []
};
this.fetchData = this.fetchData.bind(this);}


fetchData(state, instance) {
// Whenever the table model changes, or the user sorts or changes pages, this method gets called and passed the current table model.
// You can set the `loading` prop of the table to true to use the built-in one or show you're own loading bar if you want.
this.setState({ loading: true });
// Request the data however you want.  Here, we'll use our mocked service we created earlier
requestData(state.pageSize, state.page, state.sorted, state.filtered).then(
  res => {
    // Now just get the rows of data to your React Table (and update anything else like total pages or loading)
    this.setState({
      data: res.rows,
      pages: res.pages,
      loading: false
    });
  }
);

}

One Answer

From the React Docs: You can’t use Hooks inside a class component.

You need to convert your component to a functional component.

When you have done that, there is no this, and you don't have to do any this.binds to get things working.

Then for storing and accessing state, use a series of useState hooks.

Answered by Luke Storry on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP