Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
React Native: Convert State/Context code from Functional Component to Class Component

I have a simple React-Native app setup in which I use Context to share some data throughout components. I have some code setup for functional components but I would like to convert it to Class component code. Anyone who could help me further?

All that needs to be changed is the way context in de screens and state is handled.

UserContext.js (this one doesn't need changing just putting it here for context.)

import { createContext } from 'react'

export const UserContext = createContext(null);

AccountScreen.js

export default function AccountScreen() {
    const { User, setUser } = useContext(UserContext);

    return (
        <View>
            <Text>ID: {User}</Text>
        </View>
    )
}

App.js

export default function App() {

  const [User, setUser] = useState("yessir");

    return (
      <UserContext.Provider value={{User, setUser}}>
        <PaperProvider theme={theme}>
          <NavigationContainer>
            <Stack.Navigator>
              <Stack.Screen name="Register" component={RegisterScreen} options={{ headerShown: false}}/>
              <Stack.Screen name="Login" component={LoginScreen} options={{ headerShown: false}}/>
              <Stack.Screen name="Home" component={Home} options={{ headerShown: false }} />
            </Stack.Navigator>
          </NavigationContainer>
        </PaperProvider>
      </UserContext.Provider>
    );
}

Home function for nav:

function Home(){
  return(
    <Tab.Navigator 
    barStyle={{ backgroundColor: '#191919'}}
    shifting={true}>
        <Tab.Screen name="Feed" component={HomeScreen} 
        options={{
          tabBarLabel: 'Feed',
          tabBarIcon: ({ color }) => (
            <MaterialCommunityIcons name="home" color={color} size={26} />
          )
        }}/>
        <Tab.Screen name="Explore" component={ExploreScreen} 
        options={{
          tabBarLabel: 'Explore',
          tabBarIcon: ({ color }) => (
            <MaterialCommunityIcons name="magnify" color={color} size={26} />
          )
        }}/>
        <Tab.Screen name="Account" component={AccountScreen}
        options={{
          tabBarLabel: 'Account',
          tabBarIcon: ({ color }) => (
            <MaterialCommunityIcons name="account" color={color} size={26} />
          )
        }}/>
    </Tab.Navigator>
  );
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A simple way is to write a higher-order component (HOC) with can provide user context props to any component.


import * as React from 'react';
import { createContext } from "react";

export const UserContext = createContext(null);

const withUserContext = (Component) => (props)=> {
  return (
    <UserContext.Consumer>
      {(userContext) => <Component {...props} userContext={userContext}/>
      }
    </UserContext.Consumer>
  );
};

export default withUserContext;


AccountScreen.js

import React from "react";
import withUserContext from "./withUserContext";

class AccountScreen extends React.Component {
  render() {
    const { User, setUser } = this.props.userContext;
    return (
      <View>
        <Text>ID: {User}</Text>
      </View>
    );
  }
}

export default withUserContext(AccountScreen);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!