node.js - How to pass data with server-side rendering to React-component from Node -
i'm trying pass data components. found example: http://www.tech-dojo.org/#!/articles/56b1af3e8ff769fc29f96ae8 have created class datawrapper looks in example:
import react, { component } 'react' export default class datawrapper extends component { constructor(props) { super(props) } getchildcontext () { return { data: this.props.data }; } render () { return this.props.children; } } datawrapper.childcontexttypes = { data: react.proptypes.oneoftype([ react.proptypes.object, react.proptypes.string ]).isrequired };
node-server:
app.get('*', (req, res) => { match({ routes, location: req.url }, (error, redirectlocation, renderprops) => { ... const body = rendertostring( <muithemeprovider muitheme={theme}> <datawrapper data={'somedata'}> <routercontext {...renderprops} /> </datawrapper> </muithemeprovider> ) res.render('index', { title, body })
and component want use data looks this:
export default class template extends component { constructor(props, context) { super(props, context) console.log("template context", context.data); ...
context.data undefined. missing? there other way this?
if contexttypes not defined, context empty object.
, quoting directly react docs. https://facebook.github.io/react/docs/context.html#how-to-use-context
you need add
template.contexttypes = { data: // whatever type }
Comments
Post a Comment