Comments (3)
Thanks for the feedback!
- It's pretty close. The
return
isn't really needed. You're using arrow functions insidesetCounter()
, which isn't ES5. The arguments list is wrong, which I'll address below. - The props mapper receives a single argument: the props passed down from the owner. In this case, the props are
counter
,setCounter
, and whatever additional props are passed to<CounterContainer>
So if the owner renders<CounterContainer foo="bar" bar="baz" />
, then...rest
is an object containingcounter
,foo
, andbar
. - This is taking advantage of object destructuring inside the arguments list.
The fixed ES5 example looks like this:
mapProps(function(props) {
return Object.assign({}, props, {
increment: function() { props.setCounter(function(n) { return n + 1; }); },
decrement: function() { props.setCounter(function(n) { return n - 1; }); },
});
})
As you can see, ES2015 features make a big difference here, especially arrow functions. I think it's fairly safe to assume that most people who would be interested in using Recompose will be mostly familiar with ES2015 features, since Babel is the official solution for compiling JSX.
However, I agree with you that for someone who's not as familiar with things like ES2015, or currying, or function composition, or higher-order components... it may be all a bit overwhelming. Perhaps when I have the time to build a proper docs site, we can do something similar to what @gaearon did with his code snippets for React DnD, which can be toggled between ES5/ES6/ES7.
from recompose.
This is pretty accurate, yes. You can't return an object inline with arrow functions since the compiler can't differentiate between {
as the start of an object literal or the start of the function body. To get around that you wrap it in ()
:
// won't work
const vector = (x, y) => { x, y }
//works
const vector = (x, y) => ({ x, y })
The Notion function({ setCounter, ...reset })
is called destructuring in ES2015. Basically instead of doing
mapProps(function(props) {
var setCounter = props.setCounter
var rest = Array.prototype.slice.call(arguments, 1); // rest of the properties of `props`
})
You can just get the properties you want to modify and create a new object that merges the existing properties with the modified ones.
If it helps, here would be the ES5 non-immutable way:
mapProps(function(props) {
var setCounter = props.setCounter
var decrement = function () {
setCounter(function (n) {
return n - 1
})
}
var increment = function () {
setCounter(function (n) {
return n + 1
})
}
props.increment = increment
props.decrement = decrement
return props
})
from recompose.
@queckezz Ooh, I know you're trying to be helpful, but please don't modify the props object, even in an example. Don't want anyone to get confused and think that's okay :)
Here's that snippet updated to use a new object:
mapProps(function(ownerProps) {
var setCounter = ownerProps.setCounter
var decrement = function () {
return setCounter(function (n) {
return n - 1
})
}
var increment = function () {
return setCounter(function (n) {
return n + 1
})
}
var props = {}
props.increment = increment
props.decrement = decrement
return props
})
from recompose.
Related Issues (20)
- renderNothing should export Nothing HOT 4
- Does TypeScript 3.4 enable typing of compose HOT 1
- Remove flow types in repo and support only in flow-typed repo HOT 1
- Key advantages in Docs HOT 2
- The best way to test a component that use getContext recompose HOT 1
- Get a lifecycle componentDidMount unmounted error HOT 1
- Rewriting mapPropsStream with Hooks or new Lifecycle Methods HOT 4
- compose hoc causes to lose typings
- Remove usage of `createFactory` HOT 4
- Why does `mapProps` remove `children`? HOT 2
- node-fetch vulnerability issue (denial of service) HOT 8
- Forked, Updated, and Published HOT 1
- fbjs dependency still present in lock file HOT 3
- Support React 17 HOT 2
- Suggest alternative to branch(), renderComponent(), renderNothing(), compose(), etc
- TypeError: Cannot read properties of undefined (reading 'apply')
- Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
- New patch required to mitigate high risk vulnerability
- Maintainers HOT 1
- This is GITHUB and not the European Parliament or NATO.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recompose.