react-router icon indicating copy to clipboard operation
react-router copied to clipboard

Don't wrap with React.createElement unless needed

Open yocontra opened this issue 11 years ago • 10 comments

https://github.com/rackt/react-router/blob/master/modules/createRouter.js#L513

Currently causes react to emit a warning if you give a factory or existing ReactElement class as a handler in react-router. Very confusing for people who don't use JSX and are told by the React docs to ALWAYS wrap your components with createFactory if you want to use them without JSX, only to find out that react-router is doing wrapping you can't opt out of.

Warning: This JSX uses a plain function. Only React components are valid in React's JSX transform.

yocontra avatar Jan 27 '15 23:01 yocontra

Only problem is I don't think we can detect if you give us a ReactElement or a factory. That's why we don't tell you anywhere in our docs to give us a factory.

Maybe we could fix it with a FAQ?

mjackson avatar Jan 27 '15 23:01 mjackson

@mjackson What about an option for disabling the wrapping? If not I will probably have to fork and maintain a separate repo that doesn't wrap for you. I'm dealing with a situation where I have to support the result of createFactory

yocontra avatar Jan 28 '15 01:01 yocontra

Also causing warnings to be logged when using RouteHandler https://github.com/rackt/react-router/blob/master/modules/RouteHandlerMixin.js#L43

yocontra avatar Jan 28 '15 04:01 yocontra

What about an option for disabling the wrapping?

The only way we could unwrap your factory to get at the underlying component class would be to access the deprecated type property on the factory. The type property is also exposed on component classes, but it's deprecated there as well.

Why can't you give us component classes instead of factories? Are you using a lib that only generates factories?

mjackson avatar Jan 28 '15 06:01 mjackson

I guess we could have a <Route handlerFactory> prop. Then you could be explicit about the fact that you're giving us a factory, not a component class.

mjackson avatar Jan 28 '15 06:01 mjackson

@mjackson Yep - using a lib that generates factories. A handlerFactory param seems like a good solution

yocontra avatar Jan 28 '15 06:01 yocontra

people who don't use JSX and are told by the React docs to ALWAYS wrap your components with createFactory if you want to use them without JSX

Looking at this doc and related ones, they always tell you to do the wrapping inside the module that consumes the component (after import). So if you have a module or library that gives you a factory, it seems an anti-pattern to me and a step backwards.

Anyway, I'm not opposed to handlerFactory as a band-aid solution. But without factory.type there's no way to support transition hooks, or is there?

agundermann avatar Jan 28 '15 11:01 agundermann

That lib should not generate factories, I don't want to add API to be defensive for people using anti-patterns defined by React itself.

You could make your own version of RouteHandler though. Or even monkey patch render in your app.

ryanflorence avatar Feb 02 '15 06:02 ryanflorence

@rpflorence Is it a blessed pattern to wrap components into elements when components are given as properties instead of letting the user control it? I have a few modules that take components in the same way but the vibe I got from the react docs was to let the user do it themselves

yocontra avatar Feb 02 '15 18:02 yocontra

I think users should always get a component, not a factory, no matter where.

ryanflorence avatar Feb 02 '15 20:02 ryanflorence