【发布时间】:2015-12-30 07:57:32
【问题描述】:
到目前为止,我对属性如何通过参数从一个组件传递到另一个组件的了解程度如下
//开始:我的知识范围
假设在 A.jsx 中存在一个名为 topic 的状态变量。
我想将此传递给 B.jsx,所以我执行以下操作
B = require('./B.jsx')
getInitialState: function() {return {topic: "Weather"}}
<B params = {this.state.topic}>
在 B.jsx 中,我可以做类似的事情
module.exports = React.createClass({
render: function() {
return <div><h2>Today's topic is {this.props.params}!</h2></div>
}
})
调用时会显示“今天的主题是天气!”
//结束:我的知识范围
现在,我正在阅读 react-router 教程,代码如下:sn-ps
topic.jsx:
module.exports = React.createClass({
render: function() {
return <div><h2>I am a topic with ID {this.props.params.id}</h2></div>
}
})
routes.jsx:
var Topic = require('./components/topic');
module.exports = (
<Router history={new HashHistory}>
<Route path="/" component={Main}>
<Route path = "topics/:id" component={Topic}></Route>
</Route>
</Router>
)
header.jsx:
renderTopics: function() {
return this.state.topics.map(function(topic) {
return <li key = {topic.id} onClick={this.handleItemClick}>
<Link to={"topics/" + topic.id}>{topic.name}</Link>
</li>
})
}
其中this.state.topics 是通过 Reflux 从 imgur API 提取的主题列表。
我的问题是:params 是通过什么机制传递给 props 的 topic.jsx?在代码中的任何地方,我都没有看到上面关于“我的知识范围”部分所表达的习语。在 routes.jsx 或 header.jsx 中都没有 <Topic params = {this.state.topics} />。链接到full repo here。 React-router 文档说参数是“parsed out of the original URL's pathname”。这并没有引起我的共鸣。
【问题讨论】:
标签: javascript reactjs reactjs-flux react-router refluxjs