jmurzy / react-router-native

A routing library for React Native that strives for sensible API parity with react-router 🤖
MIT License
638 stars 44 forks source link

`createElement` behaviour not as indended #10

Closed jonathanglasmeyer closed 8 years ago

jonathanglasmeyer commented 8 years ago

I want to supply createElement to the <Router> to merge in props as documented here under createElement. The behaviour is not as intended: RouteUtils::createNavigationTree calls createElement with RouteView, which passes the additional props to the RouteView component. This component in turn does not pass these additional props to the NavigationComponent it creates. I'm not totally sure how to implement this which is why i created an issue first. I would assume that createElement should be called on NavigationComponent, not on RouteView. What do you think?

jonathanglasmeyer commented 8 years ago

See the PR for a fix for my specific, minimal setup scenario. My current setup is:

  const componentContext = /* some global state that should present on every route component */

  const routes =  <Route path='/' component={AppController} />;

  const createElement = (Component, props) => {
    const propsWithContext = {
      ...props,
      context: componentContext,
    };
    return <Component {...propsWithContext} />;
  };

  const routerProps = {
    createElement,
    routes,
    addressBar: true,
  };

  const router = <Router {...routerProps} />;
  render(router, appName);
jmurzy commented 8 years ago

You are right. I'll take a closer look and get back to you on #11 soon.

I also want to start writing unit and integration tests so it'll hopefully make refactoring a little less painful going forward. I guess better late than never, 🤕, and as usual I'd have to remind myself the following quote to not feel too terrible about it.

If you are not embarrassed by the first version of your product, you’ve launched too late.

🍺

jonathanglasmeyer commented 8 years ago

Awesome idea. :) That quote is on the wall of our office as well. 😄

dchambers commented 8 years ago

I assume this issue is related to the following error I was getting:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `RouteView`.

because it went away when I updated the version of react-router-native from ^2.0.0-alpha.0 (current version in NPM) to jmurzy/react-router-native#a9e4e01 (current master in GitHub).