0

naviate in

            useEffect(() => {
                if (me === null) {
                    navigation.navigate('Login');
                }
            },[me]);

but this error ocrrue

enter image description here

here is my code

             import React, {useCallback, useEffect, useLayoutEffect} from 'react';
            import {useDispatch, useSelector} from 'react-redux';
            import styled from 'styled-components/native';
            import {LOAD_MY_INFO_REQUEST, logoutRequestAction} from '../../../reducers/user';
            import {INITIAL_REQUEST} from '../../../reducers/post';
            import Button from '../../components/Button';
            const Container = styled.Text``;



            const Main = (navigation) => {

                
            const {me} = useSelector((state) => state?.user);

            // console.log("me:",me);


            useEffect(() => {
                if (me === null) {
                    navigation.navigate('Login');
                }
            },[me]);

                const dispatch = useDispatch();
                
                const onLogout = useCallback(() => {
                    dispatch(logoutRequestAction());
                }, []);
                
                return (
                <Container>

            <Button
                    type="primary"
                    htmlType="submit"
                    label="로그아웃"
                    style={{marginBottom: 24}}
                    onPress={onLogout}
                    />
                    
                </Container>
                );
            };

            export default Main;

i don't know what is wrong.....

1
  • Can you please show what is inside navigation prop? Commented Feb 20, 2021 at 7:11

2 Answers 2

1

You can use like this

const Main = ({
    navigation
  }) => { // use {} to object destructuring
    useEffect(() => {
      if (!me) { // just check null by using !
        navigation.navigate('Login');
      }
    }, [me]);
Sign up to request clarification or add additional context in comments.

Comments

1

Basically, components provide a navigation object but that is inside of the props object and you are trying to access navigation directly not from the props that are why it's saying the function is not defined.

You can change this

   const Main = (navigation) => {...

to this

const Main = (props) => {

// destructuring from props.

     const {navigation} = props

....

}

or

// Here you directly destructing the navigation from props
  const Main = ({navigation}) => {...

        useEffect(() => {
                if (me === null) {
                    navigation.navigate('Login');
                }
            },[me]);

.....
}

Complete code:

import React, { useCallback, useEffect, useLayoutEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import styled from "styled-components/native";
import {
  LOAD_MY_INFO_REQUEST,
  logoutRequestAction,
} from "../../../reducers/user";
import { INITIAL_REQUEST } from "../../../reducers/post";
import Button from "../../components/Button";
const Container = styled.Text``;

const Main = (props) => {
  const { navigation } = props;

  const { me } = useSelector((state) => state?.user);

  // console.log("me:",me);

  useEffect(() => {
    if (me === null) {
      navigation.navigate("Login");
    }
  }, [me]);

  const dispatch = useDispatch();

  const onLogout = useCallback(() => {
    dispatch(logoutRequestAction());
  }, []);

  return (
    <Container>
      <Button
        type="primary"
        htmlType="submit"
        label="로그아웃"
        style={{ marginBottom: 24 }}
        onPress={onLogout}
      />
    </Container>
  );
};

export default Main;

I hope this will help you out. Goodluck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.