Closed cristian-sima closed 8 years ago
Looks like what we need to do here is update the value of NotificationStack.propTypes. notifications
to allow any Iterable, not just an Array. I'm fine with this.
Do you know what the value should be changed to? I don't have any experience adding validation for Immutable objects. It looks like react-immutable-proptypes exists, but I don't want to add a dependency. Can we solve this problem otherwise?
Yep.
import {List} from "immutable";
...
proptypes = {
...
notifications: instanceOf(List)
}
Or just do not check the notifications. I think this is the best way
@cristian-sima Does react except accepts an immutable lists of elements in place of an array of elements?
@BerkeleyTrue I think you've intended to ask if it accepts
. Yes. It accepts, as the Immutable.List
has the interface of a Iterable
(which must contain a .map
, a .filter
and so on)
@BerkeleyTrue
But in either case you want to do it, I will appreciate very much if you want to solve it.
It appears all day as a strange warning and I can not turn it off
just do not check the notifications. I think this is the best way
The component should always check props. It's useful when debugging.
import {List} from "immutable";
I don't want to add any dependencies.
The only solution would be to add custom validation based off code from react-immutable-proptypes. However, this is a micro-optimization and I don't have time to add this now. If you'd like a take a stab at it @cristian-sima, that would be great.
Looking through the cod here https://github.com/facebook/immutable-js/blob/master/src/Iterable.js#L39
It doesn't look like that propType check would work outside of Immutable types so we would end up with warnings for everything other than Immutable Iterable.
It's possible to write a custom prop validation that checks for either an Array or an Iterable. Look for the customProp
example here: https://facebook.github.io/react/docs/reusable-components.html.
react-immutable-prototypes solves this nicely.
@joeybaker As mentioned above, that only checks for immutable iterables and not iterables in general
It would also mean everyone would be pulling in (bundling) react-immutable-proptypes as well as immutable whether they use it or not.
Correct, @joeybaker, but I'd like to keep the project dependency free and as @BerkeleyTrue mentioned, the check from that library is for an immutable Iterable only. To resolve this issue, we need to adapt the code from that library to be a custom prop validation that checks for both an immutable Iterable and an Array.
Could it be this simple?
function isMappable(props, propName) {
const maybeMappable = props[propName];
return !!(maybeMappable && Array.isArray(maybeMappable) && typeof maybeMappable.map === 'function');
}
Added array check
I think the first step of this would employ the use of Array.isArray
. If that condition fails, then test if the prop an immutable Iterable. I'm not sure what the best condition for the Iterable would be. If map
works, then let's use that.
Yes, checking for map
would prevent strings, which are iterable, from passing validation.
Is it working in this way?
@pburtchaell @BerkeleyTrue ?
I'm not available in terms of time to implement this feature. If you need it now, please submit a pull request!
On Oct 6, 2016 08:31, "Cristian Sima" notifications@github.com wrote:
@pburtchaell https://github.com/pburtchaell @BerkeleyTrue https://github.com/BerkeleyTrue ?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/pburtchaell/react-notification/issues/92#issuecomment-251962096, or mute the thread https://github.com/notifications/unsubscribe-auth/ADrbrGY4vNRRDfnP3IJwjmkSuqW4Dqcdks5qxPg7gaJpZM4J82-w .
I'm closing because this is a micro-optimization. If you have time to implement a dependency free prop check, @cristian-sima, please create a PR.
@pburtchaell @BerkeleyTrue I think there is no simple way of checking if that is an Iterable in a free dependency way. However, I am thinking of another way of approaching this problem.
We can create a new branch called support-immutablejs
which contains this dependency react-immutable-proptypes
(and implicity the immutable
dependecy). Taking into account that those interested in using support-immutablejs
will already use immutable
, they will not find it as a problem.
For all others, there will be the master branch which is free dependent. For any updates on master, we can update the support-immutablejs
branch from master.
What do you think?
Hey, @cristian-sima. I think this would work, but I would rather not maintain official support for Immutable. If you would like to maintain a Immutable supported fork of the project for your own purposes, please do.
Let's take this code:
The code works, but in the console I have this error:
I will lose the benefits of Immutable if I change the code to
Immutable.List().toArray (or toJS))
as presented hereAlso here.
Other comment: