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

102
Views
React Navigation redirect user based on a context state

I have added a state called roleSelected the initial value for this roleSelected is false, which presents in my AuthContext

const [roleSelected, setRoleSelected] = useState(false);

My stack navigation screens are like this,

<Stack.Screen name={routes.LOGIN} component={Login} />
<Stack.Screen name={routes.SIGNUP} component={Signup} />
<Stack.Screen name={routes.ROLE_SELECTION} component={Role} />

What I'm trying to do: So if the roleSelection is false, no matter which screen I'm currently in this stack I want to redirect the user ROLE_SELECTION route. So for this case I added the following code in my Login and Signup screens,

const Login = ({navigation}) => {
  ...
  const {roleSelected} = useContext(AuthContext);

  useEffect(() => {
    !roleSelected && navigation.navigate(routes.ROLE_SELECTION);
    console.log(roleSelected);
  }, [navigation]);

Whenever the app starts user redirects to the ROLE_SELECTION route, but still the user can click back and go to the Login screen, I don't want that to happen. Really appreciate it if somebody could help thanks.

almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Did you try instead of navigation.navigate to use navigation.reset that wipe the navigator state and replace it with a new route.

almost 4 years ago · Santiago Trujillo Report

0

Try Adding one more useEffect() with an empty dependency array into your code.

useEffect(() => {
    !roleSelected && navigation.navigate(routes.ROLE_SELECTION);
    console.log(roleSelected);
  }, []);
almost 4 years ago · Santiago Trujillo Report

0

I tried conditionally rendering the stack screens like this,

{!auth && <Stack.Screen name={routes.LOGIN} component={Login} />}
{!auth && <Stack.Screen name={routes.SIGNUP} component={Signup} />}
{auth && <Stack.Screen name={routes.ROLE_SELECTION} component={Role} />}
almost 4 years ago · Santiago Trujillo 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!