ctrlplusb / react-jobs

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

react-jobs/ssr loses HMR state #21

Closed smirea closed 7 years ago

smirea commented 7 years ago

the following keeps state between HMR updates:

import {withJob} from 'react-jobs';

@withJob(() => Promise.resolve())
export default class Foo extends React.Component {
    state = {rand: Math.random() * 1000; }
    render () { return <div>seed: {this.state.rand}</div>; }
}

however, the following loses state:

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

@withJob(() => Promise.resolve())
export default class Foo extends React.Component {
    state = {rand: Math.random() * 1000; }
    render () { return <div>seed: {this.state.rand}</div>; }
}

I suspect it's due to this line: https://github.com/ctrlplusb/react-jobs/blob/master/src/ssr/withJob.js#L98

for the SSR version, you are creating 2 wrappers, but only exporting 1 of them. HMR needs to bind to every wrapped component in order for state to be preserved

ctrlplusb commented 7 years ago

This seems to be working in the latest API release.

Example here: https://github.com/ctrlplusb/react-universally/tree/%40next/react-jobs

Edit the JobRenderer I created within the JobsRoute.js file.