首页 > 解决方案 > 使用flux-react-router发送道具

问题描述

我目前在我的 reactApp 中使用flux-react-router npm,并且在路由到url + 向组件发送道具时遇到问题。

这就是我路由到 url 的方式:onClick={()=>{ReactRouter.goTo( productpage/${item._id})}}

http://localhost:3000/productpage/5cb82c114e1efafcd06dc159

该 url 确实有效,但在我重新加载页面并重新渲染组件之前,该道具是未定义的。

我目前使用 window.location.href.split('/') 来获取 ID,但我想将它作为道具传递,但它似乎只在我重新渲染组件时才捕捉到道具,而不是在我路由到它时。

这是我在 index.js 中的组件

Flux.createRoute('/productpage/{id}',function(params){
    ReactDOM.unmountComponentAtNode(document.getElementById('root'));
    ReactDOM.render(<Provider store={store}><PersistGate loading={null} persistor={persistor}><ProductPage id={params.id}/></PersistGate></Provider>,document.getElementById('root'));
})

我只希望它在路由时发送道具,而不是重新加载页面来捕捉道具。

标签: reactjsreact-routerflux

解决方案


推荐阅读