Skip to content

Instantly share code, notes, and snippets.

@3nvi
Last active March 5, 2019 19:53
Show Gist options
  • Save 3nvi/a4e42aee50f9ab93cc8b8bc851bf3ea4 to your computer and use it in GitHub Desktop.
Save 3nvi/a4e42aee50f9ab93cc8b8bc851bf3ea4 to your computer and use it in GitHub Desktop.
// actions.js
export const fireAction = item => ({ type: 'ACTION', payload: { item } });
// ExpensiveComponent.jsx
import React, { memo } from 'react';
function ExpensiveComponent({ onClick }) {
return (
<button onClick={onClick}>click me</button>
)
}
export default memo(ExpensiveComponent);
// MyComponent.jsx
import React from 'react';
import { connect } from 'react-redux';
import { getItems } from './selectors';
function MyComponent({ item, fireActionWithItem, randomProp }) {
return (
<React.Fragment>
<button onClick={fireActionWithItem}>{item}</button>
<ExpensiveComponent onClick={fireActionWithItem} />
</React.Fragment>
)
}
const mapDispatchToProps = (dispatch, ownProps) => ({
fireActionWithItem: () => dispatch(fireAction(ownProps.item))
});
export default connect(null, mapDispatchToProps)(MyComponent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment