超级(道具)是必要的吗?
Is super(props) necessary?
我正在查看 Facebook React Native 文档,我在 State 部分的构造函数中看到了 super(props)。
我 只有当我们想在构造函数中访问 props 时,我们才使用 super(props)。
我的问题是,这里不用道具吗?
class Blink extends Component {
constructor(props) {
super(props);
this.state = {isShowingText: true};
// Toggle the state every second
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 1000);
不,它不是 necessary.You 可以有一个 class 组件,其中只有一个渲染。
示例:
class EventsContainer extends React.Component {
render() {
const futurEvents = getFuturEvents(this.props.events);
const passedEvents = getPassedEvents(this.props.events);
return this.props.loggedIn
? <div>
<NavbarContainer />
<Events
futurEvents={futurEvents}
passedEvents={passedEvents}
deleteEventInStateAndDB={this.props.deleteEventInStateAndDB}
/>
</div>
: <Redirect push to='/'/>
}
}
我正在查看 Facebook React Native 文档,我在 State 部分的构造函数中看到了 super(props)。
我
我的问题是,这里不用道具吗?
class Blink extends Component {
constructor(props) {
super(props);
this.state = {isShowingText: true};
// Toggle the state every second
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 1000);
不,它不是 necessary.You 可以有一个 class 组件,其中只有一个渲染。
示例:
class EventsContainer extends React.Component {
render() {
const futurEvents = getFuturEvents(this.props.events);
const passedEvents = getPassedEvents(this.props.events);
return this.props.loggedIn
? <div>
<NavbarContainer />
<Events
futurEvents={futurEvents}
passedEvents={passedEvents}
deleteEventInStateAndDB={this.props.deleteEventInStateAndDB}
/>
</div>
: <Redirect push to='/'/>
}
}