H
H
Hellas2017-10-11 21:20:05
JavaScript
Hellas, 2017-10-11 21:20:05

How can a component get data?

There is a component that is rendered on the server, then on the client side.
The state of the component says:

state = {
  globalData: this.props.globalData
}

...that allows you to get the necessary data on the server and render the component on it using this data, but on the client side you need to re-get this data.
What are the ways?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Ptolemy_master, 2017-10-11
@Hellas

For example, like this:
in the page code, write

<script>
var globalData = '<%=JSON.stringify(this.props.globalData)%>';
</script>

This is the code for ejs. Then on the client in your script you turn the string into an object.
var data = JSON.parse(window.globalData);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question