Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Output javascript objects? #41

Closed
unbug opened this issue May 31, 2015 · 5 comments
Closed

Output javascript objects? #41

unbug opened this issue May 31, 2015 · 5 comments

Comments

@unbug
Copy link

unbug commented May 31, 2015

How to output javascript objects so we could use it for front-end pages?
maybe like this:

<script dangerouslySetInnerHTML={{__html:`
            var data = JSON.parse({this.props.users});
            console.log(data);
            `}}/>
@unbug
Copy link
Author

unbug commented May 31, 2015

I got a solution,do I miss the better way?

<script data-data={JSON.stringify(this.props.users)} dangerouslySetInnerHTML={{__html:`
            var data = JSON.parse(document.currentScript.dataset['data']);
            console.log(data);
            `}}/>

@sophiebits
Copy link
Member

Something like this should work:

<script dangerouslySetInnerHTML={{__html: `
  var json = ${JSON.stringify(this.props.users)};
  var data = JSON.parse(json);
  console.log(data);
`}}/>

@unbug
Copy link
Author

unbug commented Jun 1, 2015

@spicyj Works like a charm!Where can I find the docs for this $?

@sophiebits
Copy link
Member

It's a feature of ES6 (backtick) template literals, which you're using.

http://updates.html5rocks.com/2015/01/ES6-Template-Strings

@unbug
Copy link
Author

unbug commented Jun 1, 2015

@spicyj That's right!How come I don't think in that way,thanks man!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants