Skip to content

fetchData() and fetchDataDeferred() behave as they should #4

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mariocasciaro
Copy link

@quicksnap
Copy link

Thanks for your work on this. This solves all open issues! But, that API issue with react-router still prevents merging =(

@mariocasciaro
Copy link
Author

OK, good news, I found a way to avoid the patch in react-router. Now only the patch in redux-simple-router is required!!!

@quicksnap
Copy link

Could you elaborate on what you changed?

@mariocasciaro
Copy link
Author

Oh sorry, yes, I amended the commit. I just used react-router.match inside history.listen, instead of using useRoutes + listenRoutes. It's essentially another way of getting the route components.

@quicksnap
Copy link

Well, great! Thanks for all the time put into this. This is pretty damn close..

@jlongster
Copy link
Owner

Dooh! I merged the other PRs first and this isn't merge-able anymore. Do you mind rebasing @mariocasciaro ?

@quicksnap
Copy link

:shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit: :shipit:

@mariocasciaro
Copy link
Author

Sure, I'll try to find some time tomorrow

@mariocasciaro
Copy link
Author

Should be ok to go now

@jlongster
Copy link
Owner

Thanks!

let lastMatchedLocBefore;
let lastMatchedLocAfter;

history.listenBefore((location, callback) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you should be using match browser-side at all. We should be using a history that has been augmented with useRoutes: https://github.com/rackt/react-router/blob/master/docs/API.md#useroutescreatehistory

In fact you pass routes into createHistory but that's not doing anything. You need to also wrap createHistory with useRoutes, and then the listen signature changes: you get (error, nextState) => {} instead, and you can just use nextState to get all the components.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but this is mainly a work-around for this: remix-run/react-router#2502
I don't think there is any practical drawback in using match here, but probably I'm missing something?
Besides, useRoutes can be applied only to listen and not to listenBefore, so unless we find another way for pulling the route components in listenBefore (e.g. react-router gets a patch), this is the only code I got working.

@jlongster
Copy link
Owner

Sorry for being so slow, last week was a holiday. I see a problem with how you are fetching data; I think it can be greatly simplified. Can you take a look?

@jlongster jlongster mentioned this pull request Nov 30, 2015
@mariocasciaro
Copy link
Author

I see that you spotted some issues with the logic I implemented, but unfortunately it's the only I got working properly with the current features available in react-router. I found that using createElement is not practical because it's not possible to use setState inside it, and match was the only public API from react-router that worked perfectly without requiring patches.
I agree that the logic could be simplified, but this is what it's working with the current libs. This is just the solution I prepared for my project and thought it could be useful to someone else, so I don't mind closing this PR and re-evaluating the solution once we got all the support we need from react-router.

@jlongster
Copy link
Owner

Sorry for being slow to respond, work is especially crazy right now. The only drawback is just complexity. If people are going to look to this as an example I want to find the best solution, and using match on the frontend in addition to <Router> is more complex than most people need.

I'd like to just play with it some more, but haven't had time. Definitely don't close this, I may merge and then make some changes after.

@quicksnap
Copy link

I agree @jlongster about this being an example of the best solution.

The ongoing issue in react-router is being discussed here now: remix-run/react-router#2614

Might be worth waiting until a resolution.

@jlongster
Copy link
Owner

Cool thanks for the link @quicksnap. FWIW I have a work week next week so I'm going to be super busy and not very responsive for the next week and a half. This project is a huge thing and I still don't understand all of it, so I'm at least going to be adding more simple examples to redux-simple-router but I'll keep an eye on this as well.

@bdefore
Copy link

bdefore commented Dec 23, 2015

@jlongster curious if there was ever a resolution for this? i'm seeing infinite loops for all routes except for '/' on version 1.0.1

@bdefore
Copy link

bdefore commented Dec 23, 2015

more specifically, to trigger the loop:

        try {
          console.log('fetchAllData');
          fetchAllData(
            renderProps.components,
            store.getState, store.dispatch,
            renderProps.location,
            renderProps.params
          ).then(() => {
            console.log('fetchAllData complete', store, renderProps);
            const component = (
              <Provider store={store} key="provider">
                <RoutingContext {...renderProps}/>
              </Provider>
            );

            const status = getStatusFromRoutes(renderProps.routes);
            if (status) {
              res.status(status);
            }
            console.log('sending response');
            res.send('<!doctype html>\n' + ReactDOM.renderToString(<CustomHtml assets={tools.assets()} component={component} store={store} headers={res._headers} />));
            console.log('response sent');
          });
        } catch (err) {
          console.error('DATA FETCHING ERROR:', pretty.render(err));
          res.status(500);
          hydrateOnClient();
        }

and then later, when I call ReactDOM.renderToString(component) it hangs. if i modify the line <RoutingContext {...renderProps}/> to <RoutingContext /> it works, although without the full props.

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

Successfully merging this pull request may close these issues.

4 participants