2017-05-08 32 views
1

我設法使我的上一個div組件上我的模板像這樣:如何從模板傳遞道具來反應根節點?

的Index.html

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Example</title> 
</head> 
<body> 

{% load render_bundle from webpack_loader %}<h1>Example</h1> 
<div id="react-app"></div> 
{% render_bundle 'main' %} 

</body> 
</html> 

我的反應程序:

Index.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import App from './App'; 

const rootElement = document.getElementById('react-app'); 
ReactDOM.render(<App />, rootElement); 

什麼是傳遞數據以反應的好方法?我正在使用Django模板

回答

1

一種常見模式是將json字符串輸出到腳本標記中,然後您可以在init函數中引用它。

例如:

// Above where you include your main bundle 
<script> 
    window.__INITIAL_STATE.__ = { your: 'DATA', encodedIn: 'json', here: true } 
</script> 

然後,可以參考在index.js window.__INITIAL_STATE__和傳遞數據的道具到您的根組件。

+0

嘿,你有一個地方,我可以在實踐中看到這個例子嗎?謝謝 – NewScientists

0

你想傳遞什麼數據作出反應? Props是允許用戶在反應中將數據從一個組件傳遞到另一個組件的概念。它不會通過外部世界的數據來反應應用程序。

我相信這個答案可以引導你正確的方向。 How to get Django and ReactJS to work together?

還閱讀以下來了解後端和前端一起工作的一些想法。 http://geezhawk.github.io/using-react-with-django-rest-framework http://www.openmindedinnovations.com/blogs/3-ways-to-integrate-ruby-on-rails-react-flux