Redux Action Tip

공부는 혼자하는 거·2021년 8월 21일
0

React Tip

목록 보기
8/24

라이브러리 하나 파악하기 참 힘들다..

https://redux-actions.js.org/api/createaction

전달받은 파라미터가 여러 개일 때는 객체를 만들어서 파라미터에 넣어 주면 된다.


export const userId = 1;
export const postId = 2;

dispatch(getPostAction({ userId, postId }));

//객체로 전달

export const getPostAction = createAction(GET_POST_REQUEST, ({ userId, postId }) => ({ userId, postId }));

/*
결과 :
{
    type: 'GET_POST_REQUEST'
    payload: {
        userId:1,
        postId:2,
    }
}
*/
	saga와 연동해서 잘 동작하는지 확인

export const detail = ({ userId, postId }) => {
  // const queryString = qs.stringify({
  //   page,
  //   username,
  //   tag,
  // });
  //return client.get(`/api/posts?${queryString}`);

  return console.log('이게 되냐?', userId, postId);
};

객체로 받고, 인수로 전달할 수도 있다..

it('should return a map of camel-cased action types to action creators', () => {
  const { actionOne, actionTwo } = createActions({
    ACTION_ONE: (key, value) => ({ [key]: value }),
    ACTION_TWO: ({ first, second }) => ([first, second])
  });

  expect(actionOne('value', 1)).to.deep.equal({
    type: 'ACTION_ONE',
    payload: { value: 1 }
  });
  expect(actionTwo({ first: 'value', second: 2 })).to.deep.equal({
    type: 'ACTION_TWO',
    payload: ['value', 2]
  });
});

https://backback.tistory.com/316?category=801894

profile
시간대비효율

0개의 댓글