-
-
Save brenonovelli/c3efd80982a14bb7e4dd513592f353b7 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 from 'react'; | |
import { useCallback, 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