이렇게 connect 함수를 호출하고 나면 또 다른 함수를 반환합니다. 반환된 함수에 컴포넌트를 파라미터로 넣어 주면 리덕스와 연동된 컴포넌트가 만들어집니다.

    위 코드를 더 쉽게 풀면 다음과 같은 형태입니다.

    const makeContainer = connect(mapStateToProps, mapDispatchToProps)
    makeContainer(타깃 컴포넌트)

    자, 이제 CounterContainer 컴포넌트에서 connect를 사용해 볼까요?

    containers/CounterContainer.js

    import React from 'react';
    import { connect } from 'react-redux';
    import Counter from '../components/Counter';
    
    const CounterContainer = ({ number, increase, decrease }) => {
      return (
        <Counter number={number} onIncrease={increase} onDecrease={decrease} />
      );
    };
    
    const mapStateToProps = state => ({
      number: state.counter.number,
    });
    const mapDispatchToProps = dispatch => ({
      // 임시 함수
      increase: () => {
        console.log('increase');
      },
      decrease: () => {
        console.log('decrease');
      },
    });
    export default connect(
      mapStateToProps,
      mapDispatchToProps,
    )(CounterContainer);

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