H
H
heIIfire2017-09-21 01:01:06
Node.js
heIIfire, 2017-09-21 01:01:06

How to use variables obtained from Node.js in React?

Well, specifically - how to use variables in JSX, how is it done, for example, in EJS <%=user%>?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2017-09-21
@maxfarseer

If you are using webpack you can use webpackDefinePlugin
For example:

new webpack.DefinePlugin({
      'ENV_API_ROOT_V1': JSON.stringify(process.env.ENV_API_ROOT_V1),
      'HTTP_PROTOCOL': JSON.stringify(process.env.HTTP_PROTOCOL),
      'WS_PROTOCOL': JSON.stringify(process.env.WS_PROTOCOL),
    }),

starting your build will look something like this:
in this case, variables will be available to you in the files: ENV_API_ROOT_V1, HTTP_PROTOCOL, WS_PROTOCOL

A
Artem Kislenko, 2017-09-21
@webwork

You can add them to the global window object, or create a json object in the template and parse it or get it via Ajax

V
Vahe, 2017-09-22
@vahe_2000

https://www.npmjs.com/package/babel-plugin-transfo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question