Skip to content

Instantly share code, notes, and snippets.

@brenonovelli
Forked from 3nvi/useDispatch.jsx
Created May 15, 2019 11:55
Show Gist options
  • Save brenonovelli/c3efd80982a14bb7e4dd513592f353b7 to your computer and use it in GitHub Desktop.
Save brenonovelli/c3efd80982a14bb7e4dd513592f353b7 to your computer and use it in GitHub Desktop.
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