Server-side variables to Client-Side with React-Engine and Express

后端 未结 2 446
一个人的身影
一个人的身影 2021-02-05 15:14

I\'m reasonably new to React/React-Engine. I have a config on the server-side that I need to pass certain values of to client-side however I have a dependency on NODE_ENV in ord

2条回答
  •  臣服心动
    2021-02-05 16:10

    An alternative to react-engine that works well with express (and any other view rendering node framework) is react-helper (https://github.com/tswayne/react-helper). It pretty much handles everything you need to do to render react components for you in any node framework. You just make an entry point (js file) for webpack (it can generate your webpack config for you) and add a line to your controller and view and your component will render on that page. You can also pass data to your react component from express and when the component binds in your browser it will have access to that server-side data.

    const component = reactHelper.renderComponent('MyComponent', {prop: config.prop}) res.render('view-to-render', {component})

    There is also express middleware for react-helper (https://github.com/tswayne/express-react-helper) that allows you to add context that is available to all views, which is convenient for config data.

    app.use(expressReactHelper.addToReactContext({configProp: config.foo}))

提交回复
热议问题