gpt4 book ai didi

javascript - 在自定义 View 中调用 super() 时,确保传递与传递组件的构造函数相同的 Prop

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:51 24 4
gpt4 key购买 nike

这是我生成的代码

Warning: LoaderView(...): When calling super() in LoaderView, make sure to pass up the same props that your component's constructor was passed.

import * as React from "react";
import {ActivityIndicator, Text, View} from 'react-native'

export class LoaderView extends React.Component {

constructor(props) {
super(props)
}

props = {
title: "",
loading: false
}


render() {
if (this.props.loading) {
return <View {...this.props} style={{flex: 1, justifyContent: 'center'}}>
<ActivityIndicator
size={"large"}
animating={true}/>

<Text
style={{textAlign: 'center', fontSize: 20, fontWeight: '500'}}>
{this.props.title}
</Text>
</View>
} else {
return <View {...this.props} style={{flex: 1, justifyContent: 'center'}}>
<Text
style={{textAlign: 'center', fontSize: 20, fontWeight: '500'}}>
{this.props.title}
</Text>
</View>
}
}

}

如何解决上面代码中的这个警告?

最佳答案

使用 defaultProps 而不是 props

static defaultProps = {
title: "",
loading: false
}

关于javascript - 在自定义 View 中调用 super() 时,确保传递与传递组件的构造函数相同的 Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47448907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com