ctrlplusb / react-jobs

Asynchronously resolve data for your components, with support for server side rendering.
MIT License
166 stars 34 forks source link

Cannot read property 'nextJobID' of undefined #23

Closed romanenko closed 7 years ago

romanenko commented 7 years ago

First of all, thanks for the great library and an amazing react-universally starter kit!

I'm in the process of migrating of my project, based on react-universally@12 to react-universally@next(v13). I'm following and implementing changes, according PR here: https://github.com/ctrlplusb/react-universally/pull/366.

There is an Error thrown when during SSR, when require and use withJob():

// MyComponent.js
import { withJob } from 'react-jobs/ssr';
// ...

export default withJob(/* ...my job */)(MyComponent);
// reacrApplication/index.js
  const app = (
    <Provider store={store}>
      <StaticRouter location={request.url} context={reactRouterContext}>
        <App />
      </StaticRouter>
    </Provider>
  );

  withAsyncComponents(app)
    .then(asyncComponents =>
      runJobs(asyncComponents.appWithAsyncComponents)
        .then(withJobs => ({ asyncComponents, withJobs })),
    )
    .then(({ asyncComponents, withJobs }) => {
      const html = renderToStaticMarkup(
        <ServerHTML
          reactAppString={renderToString(withJobs.appWithJobs)}
// ...
TypeError: Cannot read property 'nextJobID' of undefined
    at getJobID (/myApp/node_modules/react-jobs/commonjs/ssr/withJob.js:75:38)
    at ComponentWithJobID (/myApp/node_modules/react-jobs/commonjs/ssr/withJob.js:79:17)
    at reactTreeWalker (/myApp/node_modules/react-tree-walker/commonjs/index.js:84:15)
    at reactTreeWalker (/myApp/node_modules/react-tree-walker/commonjs/index.js:89:7)
    at reactTreeWalker (/myApp/node_modules/react-tree-walker/commonjs/index.js:89:7)
    at /myApp/node_modules/react-tree-walker/commonjs/index.js:104:11
    at forEachSingleChild (/myApp/node_modules/react/lib/ReactChildren.js:51:8)
    at traverseAllChildrenImpl (/myApp/node_modules/react/lib/traverseAllChildren.js:77:5)
    at traverseAllChildrenImpl (/myApp/node_modules/react/lib/traverseAllChildren.js:93:23)
    at traverseAllChildren (/myApp/node_modules/react/lib/traverseAllChildren.js:172:10)

https://github.com/ctrlplusb/react-jobs/blob/master/src/ssr/withJob.js#L60

This error happens server-side, and my investigations hints me, that, maybe, context does not get injected for some reason.

I decided to fill an issue here, in this project, because, even if I'm doing something wrong, in my opinion, we should handle and catch this exception somehow inside react-jobs.

romanenko commented 7 years ago

I've managed to avoid this error by disabling async-components. But I'm sure, proper error handling would be a great addition to this case.

novascreen commented 7 years ago

same issue, not sure how to combine createAsyncComponent and withJob

ctrlplusb commented 7 years ago

I have a solution coming soon. I had planned for it to be released the beginning of this week, but got swept away with life stuff. Will keep you all posted.

mattoni commented 7 years ago

I'm also interested in a solution to this. So far w/ async component is great for server side, I still want to fetch and pre-render data with React Router v4 and have been at a loss as to how to do it without static routes (which I think defeats the purpose of the new router API)

ctrlplusb commented 7 years ago

Hey all!

I have rewritten react-jobs. It now has complete interop with react-async-component.

See a demo here: https://react-universally-jobs.now.sh

It's on a branch within react-universally.

🎉

novascreen commented 7 years ago

@ctrlplusb could you double check that branch link please? i don't see a next/react-jobs branch

ctrlplusb commented 7 years ago

Sorry, I renamed the branch.

novascreen commented 7 years ago

@ctrlplusb sorry to bug you again, but i don't see any reference to react-jobs in that branch. I almost got it working, but I don't get anything on the server even though the work function runs, so would be great to see your example.

ctrlplusb commented 7 years ago

Doh! I did something really really silly!

Coming soon!

ctrlplusb commented 7 years ago

Up! :)

novascreen commented 7 years ago

awesome thanks @ctrlplusb everything works. Love your work on these tough problems!