Skip to content

Instantly share code, notes, and snippets.

@marsbomber
Created February 9, 2018 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marsbomber/1b6efd125f31e1ff2632985e0aaf439e to your computer and use it in GitHub Desktop.
Save marsbomber/1b6efd125f31e1ff2632985e0aaf439e to your computer and use it in GitHub Desktop.
// utils/partial.js
export const partial = (func, ...args) => func.bind(null, ...args);
// actions/maths.js
export const addNumberOneAndNumberTwo = (first, second) => {
return {
type: 'SOMETHING THE REDUCER CAN CATCH',
first,
second,
};
};
// containers/entry.js
import Entry from '../components/entry';
// other redux stuff
import { addNumberOneAndNumberTwo } from '../actions/maths';
const mapDispatchToProps = (dispatch) => {
return {
addNumberOneAndNumberTwo: (first, second) => {
// you can see here I do NOTHING, just push the args I receive into the
// actual action creator func call
dispatch(addNumberOneAndNumberTwo(first, second));
}
};
};
connect(mapStateToProps, mapDispatchToProps)(Entry);
// components/entry.js
import React, { Component } from 'react';
import NextLevel from './next_level';
class Entry extends Component {
render() {
return (
<div>
<NextLevel
clickHandler={partial(this.props.addNumberOneAndNumberTwo, 1)}
/>
</div>
);
}
}
export default Entry;
// components/next_level.js
import React, { Component } from 'react';
class NextLevel extends Component {
fireIt() {
this.props.clickHandler(2);
}
render() {
return (
<div>
<button
onClick={this.fireIt}
>
Click me
</button>
</div>
);
}
}
export default NextLevel;
@tommyli
Copy link

tommyli commented Feb 12, 2018

Line 39 and 52: where do you normally get the parameters from? What if values 1 and 2 are already in the store?

Right now, I have those values passed in from mapStateToProps. Do you use local state for these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment