Last active
February 3, 2020 13:05
-
-
Save 3nvi/91fad50be082f9c43fd19eeab9ea3460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useCallback } from 'react'; | |
import { useDispatch } from 'react-redux'; | |
import { increaseCounterAction } from './actions'; | |
import ExpensiveComponent from './ExpensiveComponent'; | |
// normal way | |
const Component = props => { | |
const dispatch = useDispatch(); | |
return ( | |
<button onClick={() => dispatch(increaseCounterAction()}> | |
Increase the Counter | |
</button> | |
) | |
} | |
// performance optimised way | |
const Component = props => { | |
const dispatch = useDispatch(); | |
const handleIncreaseCounter = useCallback( | |
() => dispatch(increaseCounterAction()), | |
[dispatch] | |
); | |
return <ExpensiveComponent onClick={handleIncreaseCounter} /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@3nvi 😉