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

No way to use wrapperComponent: false with AnimatedSwitch #83

Open
jvgomg opened this issue Nov 21, 2017 · 4 comments
Open

No way to use wrapperComponent: false with AnimatedSwitch #83

jvgomg opened this issue Nov 21, 2017 · 4 comments

Comments

@jvgomg
Copy link

jvgomg commented Nov 21, 2017

I have a use case where I need fine control over styles – specifically I need to add more styles to the element with the tweening styles. { wrapperComponent: false } seemed like the perfect option as it uses cloneElement and injects the styles.

However it turns out there is noway to consume the injected style prop (at least with AnimatedSwitch) – cloned element is actually an internal provided Switch component.

Is there something I am missing?

I came up with a work around I thought I would share using recompose and React context.

const WithStyleContext = withContext({ style: PropTypes.object }, ({ style }) => ({ style }))(({ children }) => children)

const StyleWrapper = getContext({ style: PropTypes.object })('div')

<AnimatedSwitch
  wrapperComponent={WithStyleContext}
  children={[
    <Route 
      children={() =>
        <StyleWrapper children="Hello world!" />
      }
    />
  ]}
/>

Is there a way to provide the style prop without needing to use context?

Thanks for the awesome library! :)

@Jeffwhen
Copy link

I think I figured it out. In RouteTransition, props is cloned into component, which is wrapped by a Route. The Route class does not pass props down.

@jvgomg
Copy link
Author

jvgomg commented Nov 24, 2017

This appears to be by design remix-run/react-router#4627

I think it would make sense to export an AnimatedRoute component with this lib which provides this enhancement to React Router’s Route.

@Jeffwhen
Copy link

Jeffwhen commented Nov 24, 2017

I replaced that Route element with this:

const TransRoute = ({component, style, ...props}) => (
  <Route {...props} render={props => React.cloneElement(component, {style, ...props})} />
);

And it works.

@jvgomg
Copy link
Author

jvgomg commented Nov 27, 2017 via email

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