Comments (2)
답변 감사합니다! context를 사용하는 게 처음이다보니 서툴러서 발생한 문제인 줄 알았는데, 당연한 것이었군요.
로컬 스토리지를 이용하는 코드도 첨부해주셔서 감사합니다. 참고하여 관련된 부분 찾아보고, 코드에도 반영해보겠습니다!(아마 다음 과제에..)
from seminar-2022.
react-router가 제공하는 Link와 navigate 등을 통해 링크를 바꾸는 경우, 일반적인 a 태그처럼 새로 페이지를 불러오는 것이 아니라 주소값만 바꾸어 새로 렌더링하게 됩니다. 하지만 주소창을 직접 수정하여 이동하는 경우 기본적으로 모든 HTML+CSS+JS를 서버에서 새로 불러오게 됩니다. 새로고침할 때처럼 내부적으로 저장되어있던 useState나 context의 모든 상태들도 초기화되는 것이 당연합니다.
이를 막으려면 로컬스토리지나 쿠키에 필요한 값을 저장하여 페이지 전환 간에도 상태를 유지할 수 있습니다. 이번 과제에서 해당 부분은 채점하지 않겠지만 연습 삼아 해보시는 것도 괜찮을 것 같습니다. setLoginObj
를 그대로 넘기 않고 스토리지에 저장하도록 persistLoginObj
로 한번 감싸서 provider의 value로 넣어줄 수 있겠습니다.
useEffect(() => {
const storedLoginObj = localStorage.getItem("loginObj"));
setLoginObj(JSON.parse(storedLoginObj));
}, []); // 빈 dependency array를 설정하면 마운트될 때 한번만 실행됩니다.
function persistLoginObj(newLoginObj) {
setLoginObj(newLoginObj);
localStorage.setItem("loginObj", JSON.stringify(newLoginObj));
}
from seminar-2022.
Related Issues (14)
- 질문 등록 방법
- 세미나 참여 기준과 관련하여 질문 드립니다. HOT 1
- Android Codelab 링크 관련 질문드립니다. HOT 2
- Storyboard 없이 SceneDelegate에 첫 시작 ViewController 세팅하는 방법
- React 세미나 1 update함수 오류 HOT 2
- React 세미나1 상세뷰 만들기 HOT 1
- React 세미나 1 가격 입력 시 쉼표 생성 관련 HOT 2
- Apple 공식 픽토그램 (SF Symbol) 사용법
- docker mysql 연결 문제 질문 HOT 2
- Kotlin으로 풀어보는 20.5기 코테 3번 문항에 대한 질문 HOT 1
- ArgumentResolver에서 request에 지정한 attribute 꺼내오기
- React 세미나2 로그인 안된 상태에서는 접근금지 HOT 6
- React 과제 2 공통 피드백
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from seminar-2022.