첫 번째 방법은 Button 컴포넌트에서 withRouter를 사용하는 것입니다.

    components/common/Button.js

    import React from 'react';
    import styled, { css } from 'styled-components';
    import { withRouter } from 'react-router-dom';
    import palette from '../../lib/styles/palette';
    
    const StyledButton = styled.button`
      border: none;
      border-radius: 4px;
      font-size: 1rem;
      font-weight: bold;
      padding: 0.25rem 1rem;
      color: white;
      outline: none;
      cursor: pointer;
    
      background: ${palette.gray[8]};
      &:hover {
        background: ${palette.gray[6]};
      }
    
      ${props =>
        props.fullWidth &&
        css`
          padding-top: 0.75rem;
          padding-bottom: 0.75rem;
          width: 100%;
          font-size: 1.125rem;
        `}
    
      ${props =>
        props.cyan &&
        css`
          background: ${palette.cyan[5]};
          &:hover {
            background: ${palette.cyan[4]};
          }
        `}
    `;
    
    const Button = ({ to, history, ...rest }) => {
      const onClick = e => {
        // to 있다면 to 페이지 이동
        if (to) {
          history.push(to);
        }
        if (rest.onClick) {
          rest.onClick(e);
        }
      };
      return <StyledButton {...rest} onClick={onClick} />;
    };
    
    export default withRouter(Button);

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.