리덕스와 리듀서 콘솔에는 찍히는데 값이 변경이 안됩니다 ㅠ
413
작성한 질문수 26
//reducers/index.js
export const loginAction = (data) => {
console.log(data)
return {
type: 'LOG_IN',
data,
}
}
export const logOutAction = () => {
return {
type: 'LOG_OUT',
}
}
const initialState = {
user : {
isLoggenIn: false,
user: null,
signUpData: {},
loginData: {},
},
post: {
mainPost: []
}
};
// ( 이전상태, 액션 ) => 다음상태
const rootReducer = (state = initialState, action) => {
console.log(action.type)
switch (action.type) {
case 'LOG_IN':
return {
...state,
user: {
...state.user,
isLoggedIn: true,
user: action.data
}
}
case 'LOG_OUT':
return {
...state,
user: {
...state.user,
isLoggedIn: false,
user: null,
}
}
default:
return state
}
}
export default rootReducer//스토어
import { createWrapper } from 'next-redux-wrapper'
import { legacy_createStore as createStore } from "redux";
import reducer from '../reducers/index'
const configureStore = () => {
const store = createStore(reducer);
return store;
}
const wrapper = createWrapper(configureStore, {
debug: process.env.NODE_ENV === 'devlopment',
});
export default wrapperaction에 type으로는 콘솔이 찍히는데 isLoggedin이 안바뀌네요... 어디가 잘못됬을까요 state값이 변경이 안됩니다. ture에서 false로 바뀌어야 하는데 설명 부탁드립니다.
import React, { useState } from 'react'
import Link from 'next/link'
import LoginForm from './LoginForm'
import UserProfile from './UserProfile'
import { Menu, Input, Row, Col } from 'antd'
import styled from 'styled-components'
import { useSelector } from 'react-redux'
const SearchInput = styled(Input.Search)`
vertical-align: middle;
`;
const AppLayout = ({ children }) => {
const { isLoggenIn } = useSelector((state) => state.user)
console.log(isLoggenIn)
return (
<div>
<Menu mode='horizontal'>
<Menu.Item>
<Link href="/">노드버드</Link>
</Menu.Item>
<Menu.Item>
<Link href="/profile">프로필</Link>
</Menu.Item>
<Menu.Item>
<SearchInput enterButton />
</Menu.Item>
<Menu.Item>
<Link href="/signup">회원가입</Link>
</Menu.Item>
</Menu>
<Row gutter={8}>
<Col xs={24} md={6}>
{isLoggenIn ? <UserProfile /> : <LoginForm />}
</Col>
<Col xs={24} md={12}>
{children}
</Col>
<Col xs={24} md={6}>
<a href="http://www.naver.com" target={'_blank'} rel="noreferre noopener">향훈</a>
</Col>
</Row>
</div>
)
}
export default AppLayoutimport React, { useCallback, useState } from 'react';
import { Form, Input, Button } from 'antd';
import Link from 'next/dist/client/link';
import styled from 'styled-components'
import { useDispatch } from 'react-redux';
import { loginAction } from '../reducers';
const LoginForm = () => {
const dispatch = useDispatch()
const [id, setId] = useState("")
const [password, setPassword] = useState("")
const onChangeId = useCallback((e) => {
setId(e.target.value);
}, []);
const onChangePw = useCallback((e) => {
setPassword(e.target.value);
}, []);
const onSubmitForm = useCallback(() => {
console.log(`id: ${id}, pw: ${password}`);
dispatch(loginAction({ id, password }))
}, [])
return (
<Form onFinish={onSubmitForm}>
<div>
<label htmlFor="user-id">아이디</label>
<br />
<Input type="text" name='user-id' value={id} onChange={onChangeId} />
</div>
<div>
<label htmlFor="user-id">비밀번호</label>
<br />
<Input type="password" name='user-id' value={password} onChange={onChangePw} required />
</div>
<ButtonWrapper>
<Button type="primary" htmlType="submit" loading={false}>로그인</Button>
<Link href="/signup"><Button>회원가입</Button></Link>
</ButtonWrapper>
</Form>
)
}
const ButtonWrapper = styled.div`
margin-top: 10px;
`
export default LoginForm
답변 1
0
onSubmitForm에서 배열에 [id, password] 하세요.
0
dispatch로 값이 action으로 값은 넘어가긴하는데 값이 변경이 안됩니다 ㅠㅠ 선생님 이제 그 콘솔찍히는건 해결이 됬습니다만 state값이 변경이 안돼는데 왜그런지 알수 있을까요 ㅠㅠ 계속 false이네요...
넥스트 버젼 질문
0
90
2
로그인시 401 Unauthorized 오류가 뜹니다
0
104
1
무한 스크롤 중 스크롤 튐 현상
0
198
1
특정 페이지 접근을 막고 싶을 때
0
117
2
createGlobalStyle의 위치와 영향범위
0
104
2
인라인 스타일 리렌더링 관련
0
98
2
vsc 에서 npm init 설치시 오류
0
159
2
nextjs 15버전 사용 가능할까요?
0
166
1
화면 새로고침 문의
0
129
1
RTK에서 draft, state 차이가 있나요?
0
164
2
Next 14 사용해도 될까요?
0
455
1
next, node 버전 / 폴더 구조 질문 드립니다.
0
359
1
url 오류 질문있습니다
0
218
1
ssh xxxxx로 우분투에 들어가려니까 port 22: Connection timed out
0
392
1
sudo certbot --nginx 에러
0
1295
2
Minified React error 콘솔에러 (hydrate)
0
481
1
카카오 공유했을 때 이전에 작성했던 글이 나오는 버그
0
257
1
프론트서버 배포 후 EADDRINUSE에러 발생
0
341
1
npm run build 에러
0
526
1
front 서버 npm run build 중에 발생한 에러들
0
399
1
서버 실행하고 브라우저로 들어갔을때 404에러
0
351
2
css 서버사이드 랜더링이 적용되지 않아서 문의 드립니다.
0
291
1
팔로워 3명씩 불러오고 데이터 합쳐주는걸로 바꾸고 서버요청을 무한으로하고있습니다.
0
251
2
해시태그 검색에서 throttle에 관해 질문있습니다.
0
207
1





