bookmark 기능 구현 중...

KoEunseo·2022년 11월 17일
0

project

목록 보기
7/37
post-custom-banner
import styled from 'styled-components';
import Button from '../atoms/Button';
import { useEffect, useState } from 'react';
import BookmarkHandle from './BookmarkHandle';

const Anchor = styled.a`
  line-height: 1;
`;

const Bookmark = () => {
  const dummyBookmark = [
    { name: 'naver', url: 'https://www.naver.com' },
    { name: 'google', url: 'https://www.google.com' },
  ];
  const [booksArr, setBookmarkArr] = useState(
    dummyBookmark
    // JSON.parse(localStorage.getItem('bookmark') || dummyBookmark)
    // [] 으로 하면 잘 되지만 새로고침하면 날아감
    // localStorage.getItem('bookmark') || 0 이나 []으로 하면 booksArr is not iterable
  );
  console.log(localStorage.getItem('bookmark') || []);
  const [isOpen, setIsOpen] = useState(false);
  let renderBookmark = [];

  useEffect(() => {
    renderBookmark = JSON.parse(localStorage.getItem('bookmark'));
  }, [booksArr]);
  return (
    <>
      {booksArr.length
        ? booksArr.map((book, i) => {
            return (
              <Button key={i} size="long" fontsize="middle">
                <Anchor href={book.url}>{book.name}</Anchor>
              </Button>
            );
          })
        : null}
      <BookmarkHandle setBookmarkArr={setBookmarkArr} booksArr={booksArr} />
    </>
  );
};

export default Bookmark;
profile
주니어 플러터 개발자의 고군분투기
post-custom-banner

0개의 댓글