How do I insert a Django template variable into a React script?
When you render the component, you should pass the pk as a prop.
<script>React.render(React.createElement(Profile, { userId: "{{ userId }}", urlPrefix: "/api/profile/" }), element);</script>
A better alternative might be to just fetch the user, and then render the component. For example, with superagent:
superagent.get('/api/profile/{{ userId }}', function(res){ React.render(React.createElement(Profile, {user: res.body} ), element);});
With browserify, you can either include data in a script tag, and use that in your code:
<script>var _appData = {userId: "{{ userId }}"};</script>
Or export modules using the -r
flag (.require()
in the api).
# shbrowserify -r react -r src/profile.js:profile.js// jsb.require('react').require('src/profile.js', {expose: 'profile.js'});
And then use the modules in regular script tags
<script>var React = require('react');var Profile = require('profile.js');React.render(React.createElement(Profile, { userId: "{{ userId }}", urlPrefix: "/api/profile/" }), element);</script>