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>
        )
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。