Session state using React.Context.
import { SessionProvider } from 'session-context';
const App = () => (
<SessionProvider>
// nested code
</SessionProvider>
);
class Header extends React.Component {
componentDidMount() {
const { updateSession } = this.props;
fetchExternalCode()
.then((data) => {
updateSession({ data });
});
}
render() {
const { session } = this.props;
return (
<div>
{ session.data }
</div>
);
}
}
Header.propTypes = {
session: PropTypes.object.isRequired, // withSession
updateSession: PropTypes.func.isRequired, // withSession
};
export default withSession(Header);
- useful when you're loading a session state from
window.__STATE__
and the like
import { SessionProvider } from 'session-context';
const App = () => (
<SessionProvider defaults={{ data: null }}>
// nested code
</SessionProvider>
);
MIT | @davalapar