강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

박지연님의 프로필 이미지
박지연

작성한 질문수

[리뉴얼] React로 NodeBird SNS 만들기

loginAction 에 대해 질문드립니다

작성

·

259

0

제로초님의 강의 잘 보고 많이 배우고 있습니다!!

이 영상을 보고 배운 코드를 바탕으로 연습을 하고있는데,

loginAction이 실행이 안되는 것 같습니다.. 따로 에러메시지도 뜨지 않아서 계속 해결이 안되어 질문드립니다..

isLoggedIn이 false에서 submit하여도 true로 바뀌지를 않습니당..

 

reducers/index.js

// 서버사이드렌더링을 위함
import { HYDRATE } from "next-redux-wrapper";
// 리듀서 합쳐주는 메서드
import { combineReducers } from "redux";

import user from "./user";
import post from "./post";

// (이전상태,액션) => 다음상태
const rootReducer = combineReducers({
  index: (state = {}, action) => {
    switch (action.type) {
      case HYDRATE:
        console.log("HYDRATE", action);
        return {
          ...state,
          ...action.payload,
        };

      default:
        return state;
    }
  },
  user,
  post,
});
export default rootReducer;

reducers/user.js

const dummyUser = {
  id: 1,
  nickname: "제로초",
  Posts: [],
  Followings: [],
  Followers: [],
};

export const initialState = {
  isLoggedIn: false,
  user: null,
  signUpData: {},
  loginData: {},
};

export const SIGN_UP = "SIGN_UP";
export const SIGN_UP_SUCCESS = "SIGN_UP_SUCCESS";
export const LOG_IN = "LOG_IN"; // 액션의 이름
export const LOG_IN_SUCCESS = "LOG_IN_SUCCESS"; // 액션의 이름
export const LOG_IN_FAILURE = "LOG_IN_FAILURE"; // 액션의 이름
export const LOG_OUT = "LOG_OUT";

export const signUpAction = (data) => {
  return {
    type: SIGN_UP,
    data,
  };
};

export const signUpSuccess = {
  type: SIGN_UP_SUCCESS,
};

export const loginAction = (data) => {
  return {
    type: LOG_IN,
    data,
  };
};
export const logoutAction = {
  type: LOG_OUT,
};
export const signUp = (data) => {
  return {
    type: SIGN_UP,
    data,
  };
};

export default (state = initialState, action) => {
  switch (action.type) {
    case LOG_IN: {
      return {
        ...state,
        isLoggedIn: true,
        user: dummyUser,
        loginData: action.data,
      };
    }
    case LOG_OUT: {
      return {
        ...state,
        isLoggedIn: false,
        user: null,
      };
    }
    case SIGN_UP: {
      return {
        ...state,
        signUpData: action.data,
      };
    }
    default: {
      return {
        ...state,
      };
    }
  }
};

pages/_app.js

import Head from "next/head";
import "../styles/global.css";

import wrapper from "../store/configureStore";

function App({ Component }) {
  return <Component />;
}
export default wrapper.withRedux(App);

pages/login.js 여기 아랫부분의 필요없는 것 같은 코드는 생략했습니다..!


import { useCallback } from "react";
import useInput from "../hooks/useInput";
import { useDispatch, useSelector } from "react-redux";
import { loginAction } from "../reducers/user";

function Login() {
  const [email, onChangeEmail] = useInput("");
  const [password, onChangePassword] = useInput("");

  const dispatch = useDispatch();

  const onSubmitForm = useCallback(() => {
    console.log(email, password);

    dispatch(
      loginAction({
        email,
        password,
      })
    );
  }, [email, password]);

  return (
    <>
      <div className={style.loginHead}>
        <Link href="/">
          <a>
            <span>
              <ImArrowLeft2 size="25" color="gray" />
            </span>
          </a>
        </Link>

        <img src={`/`} alt="" />

        <span>로그인</span>
      </div>
      <div className={style.inputLogin}>
        <form
          action="/"
          encType="multipart/form-data"
          method="post"
          onSubmit={onSubmitForm}
        >
          <h1>로그인</h1>
          <label htmlFor="user-email">이메일</label> <br />
          <input
            type="email"
            name="user-email"
            value={email}
            onChange={onChangeEmail}
            reqired
          />
          <br />
          <label htmlFor="user-password">비밀번호</label>
          <br />
          <input
            name="user-password"
            type="password"
            value={password}
            onChange={onChangePassword}
            reqired
          />
          <br />
          <button type="submit">로그인</button>
        </form>
      </div>

답변 1

0

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

redux-devtools에서는 액션이 기록되나요? console.log(email, password)한 것은 뜨나요?

박지연님의 프로필 이미지
박지연
질문자

넵! redux 툴에서는 LOG_IN 액션이 뜨고 잠깐이나마 true로 바뀌는 것 같습니다.

image컨솔로그에도 이메일과 비밀번호가 뜹니다.

리다이렉트되어서 빠르게 사라지긴하지만요...

제로초(조현영)님의 프로필 이미지
제로초(조현영)
지식공유자

지금 보니까 e.preventDefault()를 안하셨네요. 폼에서는 이거 무조건 넣으셔야겠죠?

박지연님의 프로필 이미지
박지연
질문자

헉..감사합니다!! 해결됐어용

박지연님의 프로필 이미지
박지연

작성한 질문수

질문하기