简体   繁体   中英

How can we declare and use class in react-native functional component without extending it with React.Component?

I just want to create a class in the function component. Declare the variable inside that class and use it in the function component.

Here is my app.js :

const App = () => {
   const [count, setCount] = useState(0);
   const increaseCount = () => {
     setCount(count+1)
   }
   return (
     <View>
       <Pressable onPress={() => {increaseCount()}} >
         <Text>Increase</Text>
       </Pressable>
       <Text>{count}</Text>
     </View>
   )
} 

I just want the same functionality with the class. I repeat with CLASS not CLASS COMPONENT.

I just want to create a class in the function component. Declare the variable inside that class and use it in the function component.

With comments, this satisfies your request:

const App = () => {
   const [count, setCount] = useState(0);
   const increaseCount = () => {
     setCount(count+1)
   }

   // I just want to create a class in the function component
   class Example {
     //  Declare the variable inside that class
     static message = 'hello'
   }
 
   // and use it in the function component
   const message = Example.hello;

   return (
     <View>
       <Pressable onPress={() => {increaseCount()}} >
         <Text>Increase</Text>
       </Pressable>
       <Text>{count}</Text>
     </View>
   )
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM