【发布时间】:2017-05-14 15:22:43
【问题描述】:
例如:
<Counter start="10">
...
export default class Counter extends React.Component {
constructor(props) {
super();
this.state = {
start: props.start
};
}
}
我用谷歌搜索了这个问题,我发现我找到的答案已经过时了
StackOverflow 上的问题
- ReactJS: Why is passing the component initial state a prop an anti-pattern?
- ReactJs: How to pass the initial state while rendering a component?
但我在 React 博客中找到了这篇文章:React v0.13.0 Beta 1
在那个帖子中,作者完全按照我的意愿行事,据我所知,getDefaultProps 现在已被弃用。
所以问题是:通过 props 传递状态仍然是一种反模式吗?
【问题讨论】:
-
是的。发布您需要这样做的原因,我们可以找到替代方案。
-
示例在问题中:我有一个计数器,我需要将初始状态传递给组件。如果不通过属性,我该怎么做...
标签: javascript reactjs frameworks