简体   繁体   English

react-native 如何将功能组件更改为类组件

[英]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>
    )
  }
}

source资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 react-native 道具从功能组件传递到 class 组件 - How to pass react-native props from functional component to class component 是否可以从 react-native 中的类组件访问功能上下文? - Is it possible to access functional context from class component in react-native? 如何在反应原生功能组件中应用PropType? - how to apply PropType in react-native functional component? React Native:功能组件到类组件 - React Native: Functional Component to Class Component 反应原生的 class 组件的功能组件 - functional component to class component in react native 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? 如何在反应原生功能组件中更改 header 标题? - how to change header title in react native functional component? 当 redux 连接状态改变时,有状态的本机功能组件不会重新渲染 - Stateful react-native functional component don't re-render when redux connected state change 在 react-native 功能组件中实现初始化代码的位置 - Where to implement initialization code in react-native functional component 如何在 class 组件中使用导航? REACT-NATIVE - How can I use navigation in a class component? REACT-NATIVE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM