Skip to content
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

How to wrap/unwrap dynamic components? #82

Closed
dfedynich opened this issue Jun 1, 2017 · 2 comments
Closed

How to wrap/unwrap dynamic components? #82

dfedynich opened this issue Jun 1, 2017 · 2 comments

Comments

@dfedynich
Copy link

Hello, team!

I wonder, how to manage with wrap/unwrap process, when the number of reusable components can be changed dynamically?

In you your example you assume, that we there are only two specific counters: top and bottom. But what if counters can be created/deleted dynamically? How to specify selector and wrapper in this case?

const RootComponent = () => (
  <div>
    <InstantiableConnectedCounter
      selector={state => state.topCounter} // State passed to mapStateToProps will be automatically selected using provided selector
      wrapper={type => `TopCounter.${type}`} // All the dispatched actions will be prefixed with TopCounter.
    />
    <InstantiableConnectedCounter
      selector={state => state.bottomCounter}
      wrapper={type => `BottomCounter.${type}`}
    />
  </div>
);

How should I build the reducer in the case of dynamic components?

export default buildReducer([{
  unwrapper: buildUnwrapper('Top'), // Unwraps Top prefixed actions
  handler: (state, action) => ({
    ...state,
    top: counterReducer(state.top, action)
  })
}, {
  unwrapper: buildUnwrapper('Bottom'), // Unwraps Bottom prefixed actions
  handler: (state, action) => ({
    ...state,
    bottom: counterReducer(state.bottom, action)
  })
}, {
  unwrapper: buildUnwrapper('ResetCounters'), // Direct match for ResetCounters action
  handler: (state, action) => initialState
}], initialState);

By the way, there was a similar question about this use case - #46. If the answer is similar to it, could you provide the example of the best practice how to use prsim in this case?

@Dattaya
Copy link

Dattaya commented Jun 1, 2017

@dfedynich, take a look at @tomkis' answer to my question and an example in the next message, should be enough information for you to solve this task.

@dfedynich
Copy link
Author

@Dattaya, thank you, Yaroslav, for the response and quick feedback. Your implementation to handle with dynamic components is clear. Now, I understood some of the ideas in prisma to move from the library to the set of HOC, utils and wrappers. The wise and light way in my view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants