【发布时间】:2018-05-28 11:19:23
【问题描述】:
我一直在改进我正在学习使用基于类的组件的代码。我在没有参数的函数上取得了部分成功。但是其余的都没有转换。
什么有效:
这个
const About = () => (
<div>
<h2>About</h2>
</div>
)
到
class About extends React.Component {
render() {
return (
<div>
<h2>About</h2>
<p>The content is here.</p>
</div>
);
}
}
尚未完成:
下面的组件是卡住的地方。我真的无法理解如何将这个参数传递给基于类的方法。参数会在类的构造函数中作为道具或其他东西可用吗?
const Topic = ({match}) => (
<div>
<h3>{match.params.topicId}</h3>
</div>
)
下面还有一个。
const Topics = ({match}) => (
<div>
<h2>Topics</h2>
<ul>
<li>
<Link to={`${match.url}/rendering`}>
Rendering with React
</Link>
</li>
<li>
<Link to={`${match.url}/components`}>
Components
</Link>
</li>
<li>
<Link to={`${match.url}/props-v-state`}>
Props v. State
</Link>
</li>
</ul>
<Route path={`${match.url}/:topicId`} component={Topic}/>
<Route exact path={match.url} render={() => (
<h3>Please select a topic.</h3>
)}/>
</div>
)
【问题讨论】:
标签: javascript reactjs class components