aweary / react-copy-write

✍️ Immutable state with a mutable API
MIT License
1.79k stars 54 forks source link

createSelector support multiple selectors #57

Open gingur opened 5 years ago

gingur commented 5 years ago

Feature Request

Similar to reselect, would be nice to compose selectors with other selectors, that way the business logic can be abstracted and isolated to the selector.

Example

// basic example: state.user.avatar.src
const user = createSelector(state => state.user);
const userAvatar = createSelector(user, userState => userState.avatar);
const userAvatarSrc = createSelector(userAvatar, userAvatarState => userAvatarState.src);

// advanced example: state.user.avatar.src || state.config.defaultAvatar
const config = createSelector(state => state.config);
const defaultAvatarSrc = createSelector(config, configState => configState.defaultAvatar);
const avatar = createSelector(
  userAvatarSrc,
  defaultAvatarSrc,
  (userAvatarSrcState, defaultAvatarSrcState) => (
    userAvatarSrcState || defaultAvatarSrcState
  )
);
seethroughdev commented 5 years ago

It seems like createSelector is just an identity function at this point, is there consideration to just use reselect to handle memoizing?

aweary commented 5 years ago

@seethroughtrees the idea was that createSelector would use the unstable_observedBits context API, but it's not clear if that API will ever become stable. I believe reselect already works pretty well with RCW but I'd be open to making that integration better.

seethroughdev commented 5 years ago

Makes perfect sense. Thanks for the great library @aweary .