在我的React Native 0.59 App.js中,2个属性传递给每个组件:
const data = this.props.navigation.state.params.data;
const EventWithSelf = (props) => (<Event {...props} myself={data.myself} token={data.result} />)
const NeweventWithSelf = (props) => (<NewEvent {...props} myself={data.myself} token={data.result} />)由于传入了组件可能需要的其他props的{...props},我是否必须使用构造函数显式初始化组件并运行super(props),如下所示?
export default class Event extends React.Component {
constructor(props) {
super(props);
this._isMounted = true;
this.state = {
activeEvents: [],
user: this.props.myself,
token: this.props.token,
};
};
//more code或者我也可以不使用构造函数,如下所示:
export default class Event extends React.Component {
state = {
activeEvents: [],
user: this.props.myself,
token: this.props.token,
};
//more code......... 在没有显式构造函数的情况下,在哪里初始化this._isMounted = true更好
发布于 2019-07-26 23:35:29
对于这种情况,您不需要准备好constructor。你可以这样做:
export default class Event extends React.Component {
state = {
activeEvents: [],
user: this.props.myself,
token: this.props.token,
}
_isMounted = false
}https://stackoverflow.com/questions/57223103
复制相似问题