[英]Pass props React Router
我需要將道具通過React Router傳遞到LawsList組件。 所以我想出了這段代碼:
在main.js上:
const lawlistcomponent = props => {
console.log("test");
return <LawsList {...props} />;
};
const routes = (
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute render={lawlistcomponent} />
<Route path="laws/:lawId" component={LawsMain} />
<Route path="votes/:lawId" component={VotesMain} />
</Route>
</Router>
);
在LawsList組件上:
export default createContainer(props => {
console.log(this.props.voted);
Meteor.subscribe("laws", props.voted);
return { laws: Laws.find({}).fetch() };
}, LawsList);
它根本不返回該組件。 我沒有錯誤消息可以指導!
有什么提示嗎?
您當地的lawlistcomponent
組件沒有以傳遞到路線的方式接收任何道具。 無論如何,我建議您使用render
inline函數將組件渲染到路由(假設您正在使用React Router V4)。 像這樣:
<Route
exact
path='/'
render={(props) => <LawsList {...props} anotherProp={value} />}
/>
如React 路由器文檔中所述 :
這樣可以方便地進行內聯渲染和包裝,而無需進行上述不必要的重新安裝。 無需使用組件prop為您創建新的React元素,而是可以傳遞位置匹配時要調用的函數。 渲染道具接收與組件渲染道具相同的所有路線道具。
感謝Jens的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.