-
Notifications
You must be signed in to change notification settings - Fork 47k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Props and Immutable objects? #2059
Comments
|
Well, a cloned immutable object doesn't quite work as props... None of the issues I mention are strictly an React issue. It's more of a best practice issue, i.e. immutable objects are a best practice (?) (PureRenderMixin) with React. However, React doesn't quite natively support immutable objects. Maybe something along these lines could be considered in react-future? |
The immutable objects advocated by React are simply plain objects that you don't mutate. Using immutablejs should be no problem (AFAIK) with React unless you make the props-object a non-plain object. Which is a general issue with JS, there is no facility for cloning objects, avoid using immutablejs for the props-object and it should be fine? As for |
We don't have first-class propTypes support for immutable-js, the same way we don't have propTypes support for backbone models. Until immutable stuff are built into JS (es7/8 please!) I think propTypes will have to stay this way, for the better. |
@chenglou On another note, I'm curious, would it be imagineable to turn propTypes into an addon/mixin instead? I'm guessing it couldn't use Anyway, not really pushing for it, it just seems natural to shed things that aren't intrinsic to core. |
I have an idea to share about this. Not a suggested change in the framework, but a solution for using what's there to work with Immutable objects now. (Also curious if others have feedback about what I'm doing.) I have started attaching an Immutable object to To type check I can take advantage of the existing custom validator functionality to provide some checks like this: var Immutable = require('immutable');
function impropTypeCheck(expectedType) {
return function(props, propName, componentName) {
var propType = typeof props.improps.get(propName);
if (propType !== expectedType) {
return new Error(
`Warning: Invalid prop '${propName}' of type '${propType}' ` +
`supplied to '${componentName}'; expected '${expectedType}'.`
);
}
};
}
function impropImmutableInstance(expectedConstructor) {
return function(props, propName, componentName) {
if (!(props.improps.get(propName) instanceof expectedConstructor)) {
return new Error(
`Warning: Invalid prop '${propName}' supplied to '${componentName}'; ` +
`expected instance of '${expectedConstructor.name}'.`
);
}
};
}
var impropTypeCheck = {
impropString: impropTypeCheck('string'),
impropBool: impropTypeCheck('boolean'),
immutableList: impropImmutableInstance(Immutable.List)
};
module.exports = impropTypeCheck; And in my component my propTypes: {
improps: React.PropTypes.instanceOf(Immutable.Map),
name: impropTypeCheck.impropString,
label: impropTypeCheck.impropString,
prompt: impropTypeCheck.impropString,
value: impropTypeCheck.impropString,
required: impropTypeCheck.impropBool,
choices: impropTypeCheck.immutableList,
errors: impropTypeCheck.immutableList
}, |
Yeah, the practice that I've been using is to refer to the <MyThing data={immutableMap} /> I usually do similar with state being an object with a single key. For propTypes I use a technique similar to @davidtheclark's proposal. Object spread operator ( <div key="foo" ...myProps />
// becomes
React.createElement('div', {key:"foo" ...myProps})
// becomes
React.createElement('div', Object.assign({key:"foo"}, myProps)) I don't think it's likely that props will move from Object to Map anytime soon, but it's certainly possible to create a custom version of JSX transpiler which generates Maps instead of Objects. Mapping over collections (Immutable or ES6 or otherwise) has been possible in React master branch for some time now and will be included in v0.13
|
From my understanding that seems counter-intuitive, isn't props what Objects are intended for? A rather static set of keys, used over and over... whereas Maps are for dynamic lookups? |
Closing as this doesn't seem actionable for us. |
I've started using immutablejs together with reactjs.
However, I've noticed that passing around immutable objects doesn't quite fit together with reactjs, i.e. {...myObject}, {myObjects.map(object => React.DOM.div()} and propTypes don't quite support them without first converting to JS object which has an unnecessary overhead.
Any thoughts or advice on this issue?
The text was updated successfully, but these errors were encountered: