React-Router在Go中如何使用子组件?

huangapple go评论151阅读模式
英文:

React-Router with child in Go how does it work?

问题

我正在尝试在Go服务器中使用react-router。

我进行了一些测试,但是我无法实现我想要的效果。

我的React组件:

  1. var App = React.createClass({
  2. render: function(){
  3. return (
  4. <div>
  5. <h2>App</h2>
  6. <li><Link to="/page1">Page 1</Link></li>
  7. <li><Link to="/page2">Page 2</Link></li>
  8. </div>
  9. )
  10. }
  11. })
  12. var Page1 = React.createClass({
  13. render: function(){
  14. return (
  15. <div>
  16. <h2>Page 1</h2>
  17. <li><Link to="/page2">Page 2</Link></li>
  18. </div>
  19. )
  20. }
  21. })
  22. var Page2 = React.createClass({
  23. render: function(){
  24. return (
  25. <div>
  26. <h2>Page 2</h2>
  27. <li><Link to="/page1">Page 1</Link></li>
  28. </div>
  29. )
  30. }
  31. })

我的React路由:

  1. ReactDOM.render((<Router history={browserHistory}>
  2. <Route path="/" component={App}>
  3. <Route path="page1" component={Page1} />
  4. <Route path="page2" component={Page2} />
  5. </Route>
  6. </Router>), document.getElementById('app'))

我的Go服务器:

  1. func Render(c web.C, w http.ResponseWriter, r *http.Request) {
  2. RenderHTMLPage(w, "index.html")
  3. }
  4. func main() {
  5. goji.Get("/page1", Render)
  6. goji.Get("/page2", Render)
  7. goji.Get("/", Render)
  8. goji.Serve()
  9. }

我的问题是,当我在'App'组件中点击链接时,例如:

  1. <li><Link to="/page1">Page 1</Link></li>

URL会改变,但实际组件仍然是App而不是Page1,所以URL改变了但组件没有改变。如果我直接加载URL,结果也是一样的。

有人可以帮助我吗?谢谢 React-Router在Go中如何使用子组件?

英文:

I'm trying to use react-router with a server in Go.

I did some tests but I can not do what I want.

My react components :

  1. var App = React.createClass({
  2. render: function(){
  3. return (
  4. &lt;div&gt;
  5. &lt;h2&gt;App&lt;/h2&gt;
  6. &lt;li&gt;&lt;Link to=&quot;/page1&quot;&gt;Page 1&lt;/Link&gt;&lt;/li&gt;
  7. &lt;li&gt;&lt;Link to=&quot;/page2&quot;&gt;Page 2&lt;/Link&gt;&lt;/li&gt;
  8. &lt;/div&gt;
  9. )
  10. }
  11. })
  12. var Page1 = React.createClass({
  13. render: function(){
  14. return (
  15. &lt;div&gt;
  16. &lt;h2&gt;Page 1&lt;/h2&gt;
  17. &lt;li&gt;&lt;Link to=&quot;/page2&quot;&gt;Page 2&lt;/Link&gt;&lt;/li&gt;
  18. &lt;/div&gt;
  19. )
  20. }
  21. })
  22. var Page2 = React.createClass({
  23. render: function(){
  24. return (
  25. &lt;div&gt;
  26. &lt;h2&gt;Page 2&lt;/h2&gt;
  27. &lt;li&gt;&lt;Link to=&quot;/page1&quot;&gt;Page 1&lt;/Link&gt;&lt;/li&gt;
  28. &lt;/div&gt;
  29. )
  30. }
  31. })

My react routing :

  1. ReactDOM.render((&lt;Router history={browserHistory}&gt;
  2. &lt;Route path=&quot;/&quot; component={App}&gt;
  3. &lt;Route path=&quot;page1&quot; component={Page1} /&gt;
  4. &lt;Route path=&quot;page2&quot; component={Page2} /&gt;
  5. &lt;/Route&gt;
  6. &lt;/Router&gt;), document.getElementById(&#39;app&#39;))

my go server :

  1. func Render(c web.C, w http.ResponseWriter, r *http.Request) {
  2. RenderHTMLPage(w, &quot;index.html&quot;)
  3. }
  4. func main() {
  5. goji.Get(&quot;/page1&quot;, Render)
  6. goji.Get(&quot;/page2&quot;, Render)
  7. goji.Get(&quot;/&quot;, Render)
  8. goji.Serve()
  9. }

My problem is when i click on Link in 'App' Component, for example :

  1. &lt;li&gt;&lt;Link to=&quot;/page1&quot;&gt;Page 1&lt;/Link&gt;&lt;/li&gt;

url changes like that

> http://localhost:8000/page1

But actual component is already App and not Page1, so url changes but not component.
And if i load directly url, i have the same result.

Does anyone help me ?
Thank's React-Router在Go中如何使用子组件?

答案1

得分: 1

这不是一个关于Go的问题,而是一个关于React的问题。你需要渲染App组件的子组件,以便其他两个组件能够显示出来。以下是从React Router文档中摘录的代码片段:

  1. const routes = (
  2. <Route component={App}>
  3. <Route path="groups" component={Groups} />
  4. <Route path="users" component={Users} />
  5. </Route>
  6. )
  7. class App extends React.Component {
  8. render () {
  9. return (
  10. <div>
  11. {/* 这里将是<Users>或<Groups> */}
  12. {this.props.children}
  13. </div>
  14. )
  15. }
  16. }

你需要在App组件中使用this.props.children来渲染子组件。

英文:

This isn't a go question, but a react question, you need to render the App component's children in order for the other two components to display, a la this code snippet taken from the docs for React Router (https://github.com/reactjs/react-router/blob/master/docs/API.md#props-2):

  1. const routes = (
  2. &lt;Route component={App}&gt;
  3. &lt;Route path=&quot;groups&quot; component={Groups} /&gt;
  4. &lt;Route path=&quot;users&quot; component={Users} /&gt;
  5. &lt;/Route&gt;
  6. )
  7. class App extends React.Component {
  8. render () {
  9. return (
  10. &lt;div&gt;
  11. {/* this will be either &lt;Users&gt; or &lt;Groups&gt; */}
  12. {this.props.children}
  13. &lt;/div&gt;
  14. )
  15. }
  16. }

huangapple
  • 本文由 发表于 2016年3月30日 03:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/36293338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定