cerebral-legacy / cerebral-view-react

React View layer package for Cerebral
16 stars 9 forks source link

Mapper decorator #33

Closed Guria closed 8 years ago

Guria commented 8 years ago

Just dumping some ideas

@Mapper(['activeModule'], 'rootComponent', {
    'inbox': Inbox,
    'calendar': Calendar,
    'todo': Todos
})
class App extends Component {
  var Component = this.props.rootComponent;
  render () {
    return (
      <div>
        <Navbar />
        <Component />
      </div>
    );
  }
}
christianalfoni commented 8 years ago

Just to bringing in a different perspective :-)

const components = {
  'inbox': Inbox,
  'calendar': Calendar,
  'todo': Todos
};

@Cerebral({
  activeModule: ['activeModule']
})
class App extends Component {
  render () {
    const Component = components[this.props.activeModule];

    return (
      <div>
        <Navbar />
        <Component />
      </div>
    );
  }
}
Guria commented 8 years ago

Thats a lot more better. Great snippet for the docs, in fact.

tnrich commented 8 years ago

Would the active module be defined by the url? Can you store multiple modules in the central state?

NervosaX commented 8 years ago

You could use something like lodash' get command for some sort of nested version:

const components = {
  'contact': {
    'list': ListComponent,
    'detail': DetailComponent
  }
};

And retrieve like so:

const Component = _.get(components, ["contact", "list"]);
christianalfoni commented 8 years ago

I will close this as we are exploring a cerebral-modules concept. Look out for it in near future :-)