Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
How to access props data from different screen in React Native

New to React Native and trying to figure out the right way to access props from another screen.
Most of the answers I found online were all very different, so I'm wondering if there's a recommended way to do it.

App.js - Here user is the currently connected user's data.

{ user ? (
          <Stack.Screen name="Home">
            {props => <HomeScreen {...props} extraData={user} />}
          </Stack.Screen>
        ) : (...)
}

On other screens, I'm sending the user to HomeScreen this way:

navigation.navigate('Home', {user: user})

HomeScreen.js - In this function, I want to access the user's data from App.js, or any other screen that sends the user.

export default function HomeScreen(props, {navigation}) {...}

How should I structure my code in HomeScreen.js to receive the user correctly?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'd recommend using Context API, documentation about that can be found here

Here's an example usage:

Create a provider for the user details

// Import createContext from react
import {createContext} from 'react'

// Create a context that holds a user and a function to change the user details
export const UserContext = createContext({user: null, setUser: (user) => {}})

Wrap the main navigator that contains your home screen with the provider from UserContext

function MainNavigator() {
  // For state management, useState for the user
  const [user, setUser] = useState(null)

  return (
    // Pass the user and setUser inside the Provider value for consumption
    <UserContext.Provider value={{user, setUser}}>
      <YourNavigator>
        { /* Your screen */ }
        <Stack.Screen name="home" component={HomeScreen} />
      </YourNavigator>
    </UserContext.Provider>
  )
}

Consume the context inside your HomeScreen component using a simple hook

import { useContext } from 'react'
import { UserContext } from '../where/your/provider/is'

export default function HomeScreen(props) {
  // Consume the context!
  const { user, setUser } = useContext(UserContext)

  return <>{ /* Your screen layout */}</>
}

This is just a simple example for this particular use case, I suggest learning more about Context API and play around with it

Hope you find this helpful

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda