[英]react-native how to change functional component to class component
I need to change functional component to class component.我需要将功能组件更改为类组件。 I know how to do this but if I want to pass props how would I do it?我知道该怎么做,但如果我想传递道具,我该怎么做?
Functional component功能组件
const FunctionalComponent = (props) => {
return (
<View>
<View>
<TouchableOpacity
onPress={()=> {props.navigation.navigate('profile')}}>
<Text>button</Text>
</TouchableOpacity>
</View>
<ScrollView>
<DrawerItems {...props}/>
</ScrollView>
</View>
)
}
Class component类组件
class ClassComponent extends Component = (props) => {
render(){
return (
<View>
<View>
<TouchableOpacity
onPress={()=> {props.navigation.navigate('profile')}}>
<Text>button</Text>
</TouchableOpacity>
</View>
<ScrollView>
<DrawerItems {...props}/>
</ScrollView>
</View>
)
}
}
I tried this but this will throw me an error.我试过了,但这会给我一个错误。
Try this: constructor in react source .试试这个:react source中的构造函数。
class ClassComponent extends Component {
render() {
return (
<View>
<View>
<TouchableOpacity
onPress={() => { this.props.navigation.navigate('profile') }}>
<Text>button</Text>
</TouchableOpacity>
</View>
...
//Other stuff
....
</View>
)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.