V
V
Vakzinator2019-03-02 20:17:05
React
Vakzinator, 2019-03-02 20:17:05

How to pass data to a multipage react application?

Good day!
I'm trying to understand react.js and the question arose - how to transfer data to the react application correctly? I am interested in the information that is displayed on all pages of the site. Let's say the menu is in the Header. Is it possible to make this data be passed as a variable with json in the head? Something like this:

<head>

  <script>
    var menu = {
      "items": [
        {
          "title": "Главная",
          "url": "/"
        },
        {
          "title": "Каталог",
          "url": "/catalog/"
        },
      ]
    }
  </script>

  <script src="react.js"></script>
  <script src="main.js"></script>	
</head>
<body>
  <div id="app"></div>
</body>

Is it correct?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-05-01
@Vakzinator

Yes, you can do this, but it's better something like this: During initialization, this data can be intercepted into the state of the root component, or the library's state management repository if it is used.

H
hzzzzl, 2019-03-02
@hzzzzl

so if "on all pages of the site", then it's probably easier to make a separate component that will hold this data and load it from json with its own componentDidMount?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question