Using arrow function in React JS - Bind

When programming applications with React Js to handle events you need to use the bind method. There are many different ways you can do that, such as placing it in a constructor

1
2
3
4
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}

Using arrow function in React JS - Callback

When you need to perform setState in a callback function, for example call api and update the data as below

1
2
3
4
5
6
7
8
9
10
axios.get('http://yourdomain/api/get-data')
.then(function (response) {
// handle success
this.setState({
data: response.data
})
})
.catch(function (error) {
// handle error
})
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×