React Web Structure Test3

props event 전달

Posted by ETHAN KIM on April 04, 2022 · 2 mins read
React

주요 개념

  • 함수형 컴포넌트 내 부모자식간 이벤트 전달
  • Menu 컴포넌트 export시 index구분을 위해 배열형태로 전달


실습 환경

  • nodejs react 패키지


.Components/Structure/Menu.js (메인 페이지)

  • ./Routes/Main.js 로부터 변경이벤트 및 메뉴제목이 담긴 배열을 받는다.
  • 배열을 순회하며 메뉴 컴포넌트 생성
  • 각 메뉴에 OnClick이벤트로 변경 로직을 넣어준다.
import styled from "styled-components";

const StyledMenu = styled.div`
height:100px;
padding:5px 20px;
`;
const StyledUl = styled.ul`
    list-style:none;
    margin:0;
    padding:1% 8%;
`;
const StyledLi = styled.li`
    margin: 5px;
    padding: 15px;
    width: 100px;
    border : 0;
    background : white;
    color: black;
    float: left;
    font-weight:bold;
    cursor: pointer;

    box-shadow: inset 0 0 0 0 black;
    transition: color .3s ease-in-out, box-shadow .3s ease-in-out;    

    &:hover{  
        box-shadow: inset 110px 0 0 0 black;
        color: white;
    }    
`;

const Menu = (props) => {
    return(
        <>
        <StyledMenu>
            <StyledUl>
                {props.list.map((item, index) => {
                    return(
                        <>
                        <StyledLi key={index} onClick={() => props.switch_page(index)}>{item.title}</StyledLi>
                        </>
                    )
                })}
            </StyledUl>
        </StyledMenu>
        </>
    )
}

export default Menu;