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

Add scroll-behavior capability #261

Closed
catamphetamine opened this issue Jul 16, 2016 · 5 comments
Closed

Add scroll-behavior capability #261

catamphetamine opened this issue Jul 16, 2016 · 5 comments

Comments

@catamphetamine
Copy link
Contributor

catamphetamine commented Jul 16, 2016

You can add a section in the README saying that if a user wants scroll-behavior then they should first install it (react-router-scroll has scroll-behavior bundled)

npm install react-router-scroll --save

And then pass scrollBehavior={true} prop to <ReduxRouter/>

ReduxRouter.js

...

class ReduxRouterContext extends Component {
    static propTypes = {
        location: PropTypes.object,
        RoutingContext: PropTypes.any, // PropTypes.element won't work
    }

    render() {
        ...

        let RoutingContext = this.props.RoutingContext || DefaultRoutingContext

        if (this.props.scrollBehavior) {
            const useScroll = require('react-router-scroll')

            // Add `scroll-bevahiour` for scroll position management
            RoutingContext = (props) =>
            {
                // https://github.com/taion/react-router-scroll/blob/master/src/index.js
                return useScroll().renderRouterContext(< DefaultRoutingContext {...props}/>, props)
            }
        }

        return <RoutingContext {...this.props} />
    }
}

I tested this code and it works as intended (waits for page to fetch data asynchronously, and then does the scrolling thing).

If someone tries using bare scroll-behavor using createHistory when creating Redux store then it won't work: the page scroll will be laggy when navigating through pages, not waiting for page data to load. This method I came up with solves the issue.

@catamphetamine
Copy link
Contributor Author

Or maybe this library shouldn't incorporate scroll-behavior for simplicity's sake.
Then this issue can be a how to for people coming from google.

import useScroll from 'react-router-scroll'
import { RouterContext } from 'react-router'

// Add `scroll-bevahiour` for scroll position management
const RoutingContext = (props) =>
{
    // https://github.com/taion/react-router-scroll/blob/master/src/index.js
    return useScroll().renderRouterContext(<RouterContext {...props}/>, props)
}

<ReduxRouter RoutingContext={RoutingContext}/>

@Scarysize
Copy link
Contributor

Yep, would be better. You could also formulate a stackoverflow question and answer it yourself. This way it can be found more easily.

@perrin4869
Copy link

Wouldn't something like this be better?

import { applyRouterMiddleware } from 'react-router'

const RouterContext = applyRouterMiddleware(useScroll());
<ReduxRouter RoutingContext={RouterContext}/>

@catamphetamine
Copy link
Contributor Author

catamphetamine commented Jul 17, 2016

Oh, yes, Julian is a react-router contributor and he proposed this solution in react-router repo
remix-run/react-router#3641

@perrin4869 I've just tested your code and it seems to work. I agree that it can be considered a better solution.

@perrin4869
Copy link

@halt-hammerzeit I'm not really a contributor to react-router, just a user that has had the chance to play extensively with middlewares lol

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

3 participants