dongyun65343 2016-03-29 19:23
浏览 52
已采纳

Go中带有子元素的React-Router如何工作?

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 :

var App = React.createClass({
    render: function(){
        return (
            <div>
            <h2>App</h2>
            <li><Link to="/page1">Page 1</Link></li>
            <li><Link to="/page2">Page 2</Link></li>
            </div>
        )
    }
})

var Page1 = React.createClass({
    render: function(){
        return (
            <div>
            <h2>Page 1</h2>
            <li><Link to="/page2">Page 2</Link></li>
            </div>

        )
    }
})

var Page2 = React.createClass({
    render: function(){
        return (
            <div>
            <h2>Page 2</h2>
            <li><Link to="/page1">Page 1</Link></li>
            </div>

        )
    }
})

My react routing :

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

my go server :

func Render(c web.C, w http.ResponseWriter, r *http.Request) {
        RenderHTMLPage(w, "index.html")
}

func main() {
        goji.Get("/page1", Render)
        goji.Get("/page2", Render)
        goji.Get("/", Render)
        goji.Serve()
    }

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

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

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 :)

  • 写回答

1条回答 默认 最新

  • dpwu16132 2016-03-29 20:40
    关注

    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):

    const routes = (
      <Route component={App}>
        <Route path="groups" component={Groups} />
        <Route path="users" component={Users} />
      </Route>
    )
    
    class App extends React.Component {
      render () {
        return (
          <div>
            {/* this will be either <Users> or <Groups> */}
            {this.props.children}
          </div>
        )
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目