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

Dynamic component composition #46

Closed
biirus opened this issue Aug 29, 2016 · 3 comments
Closed

Dynamic component composition #46

biirus opened this issue Aug 29, 2016 · 3 comments

Comments

@biirus
Copy link

biirus commented Aug 29, 2016

Hi, there!

Maybe I have a very stupid question but I can't understand how to make multiple components composition when the list of components is dynamically changes.
For example I have two components:

Component
it's view:

export default view(({ model, dispatch }) => (
    <div onClick={() => dispatch({type: 'CLICK', payload: model.content}>
        {model.content}
    </div>
))

ComponentList
is's view:

export default view(({ model, dispatch }) => (
    <ul>
        <li>
            <Component model={{content: model.a}} dispatch={forwardTo(dispatch, 'A')} />
        </li>
        <li>
            <Component model={{content: model.b}} dispatch={forwardTo(dispatch, 'B')} />
        </li>
    </ul>
))

it's updater:

export default new Updater({a: null, b: null})
    .case('A', (model, action) => {...model, a: ComponentUpdater(model.a, action)}
    .case('B', (model, action) => {...model, b: ComponentUpdater(model.b, action)}
    .toReducer();

It is totally clear for me. But what should I do, if I have dynamically changed component list. Eg:

export default view(({ model, dispatch }) => (
    <ul>
        {model.list.map((item, index) => 
            <li>
                <Component model={{content: item}} dispatch={forwardTo(dispatch, index)} />
            </li>
        )}       
    </ul>
))

How should my ComponentList updater look like?

@tomkis
Copy link
Collaborator

tomkis commented Aug 29, 2016

Hello, you'd need to use parametirezedMatcher there's an entire chapter in documentation which covers exactly your use case.

Something like:

// view
export default view(({ model, dispatch }) => (
    <ul>
        {model.list.map((item, index) => 
            <li>
                <Component model={{content: item}} dispatch={forwardTo(dispatch, 'Component', index)} />
            </li>
        )}       
    </ul>
));

// updater
import { Updater, Matchers } from 'redux-elm';

export default new Updater({ components: [] })
  .case('Component', (model, action) => {
    const numericComponentIndex = parseInt(action.matching.args.param, 10);

    return {
      ...model,
      components: model.components.map((componentModel, index) => {
        if (index === numericComponentIndex) {
          return componentUpdater(componentModel, action);
        } else {
          return componentModel;
        }
      })
    };
  }, Matchers.parameterizedMatcher)

@biirus
Copy link
Author

biirus commented Aug 29, 2016

Thanks, @tomkis1!
My bad =(

@tomkis
Copy link
Collaborator

tomkis commented Aug 29, 2016

np!

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