Skip to content

Commit

Permalink
[Fix] useCallback을 사용한 리렌더링 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
kyujonglee committed Oct 25, 2019
1 parent 978d480 commit 5265035
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 30 deletions.
46 changes: 28 additions & 18 deletions front/src/components/Personnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,36 @@ const Personnel = ({ close }) => {
});
}, [dispatch]);

const increaseLittle = useCallback(name => {
dispatch({
type: 'INCREASE_LITTLE',
name
});
}, [dispatch]);
const increaseLittle = useCallback(
name => {
dispatch({
type: 'INCREASE_LITTLE',
name
});
},
[dispatch]
);

const decreaseLittle = useCallback(name => {
dispatch({
type: 'DECREASE_LITTLE',
name
});
}, [dispatch]);
const decreaseLittle = useCallback(
name => {
dispatch({
type: 'DECREASE_LITTLE',
name
});
},
[dispatch]
);

const reset = useCallback(() => {
dispatch({
type: 'RESET'
});
}, [dispatch]);

const closeAndSave = () => {
close();
};

return (
<Container>
<PersonnelRow
Expand All @@ -83,26 +93,26 @@ const Personnel = ({ close }) => {
text={'어린이'}
age={'2~12세'}
value={child}
increase={increaseLittle.bind(null, 'child')}
decrease={decreaseLittle.bind(null, 'child')}
increase={useCallback(increaseLittle.bind(null, 'child'), [])}
decrease={useCallback(decreaseLittle.bind(null, 'child'), [])}
/>
<PersonnelRow
text={'유아'}
age={'2세 미만'}
value={baby}
increase={increaseLittle.bind(null, 'baby')}
decrease={decreaseLittle.bind(null, 'baby')}
increase={useCallback(increaseLittle.bind(null, 'baby'), [])}
decrease={useCallback(decreaseLittle.bind(null, 'baby'), [])}
/>
<Row>
<Text onClick={reset}>{adult + child + baby === 0 || '삭제'}</Text>
<Text onClick={close}>저장</Text>
<Text onClick={closeAndSave}>저장</Text>
</Row>
</Container>
);
};

Personnel.propTypes = {
close: PropTypes.func.isRequired
}
};

export default React.memo(Personnel);
16 changes: 12 additions & 4 deletions front/src/components/Room/RoomContainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import { gql } from 'apollo-boost';
import { useMutation } from '@apollo/react-hooks';
Expand Down Expand Up @@ -48,7 +48,7 @@ const RoomContainer = ({
const pDispatch = usePersonnelDispatch();

const isPerson = adult !== 0 || child !== 0 || baby !== 0;
const createReservation = async () => {
const createReservation = useCallback(async () => {
try {
if (checkIn < new Date() || !isPerson) {
throw Error();
Expand All @@ -73,7 +73,15 @@ const RoomContainer = ({
'예약에 실패하셨습니다. \n인원과 날짜를 선택했는지 확인해주세요.'
);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
checkIn,
checkOut,
createReservationMutation,
id,
pDispatch,
rDispatch
]);
return (
<RoomPresenter
createReservation={createReservation}
Expand Down Expand Up @@ -102,4 +110,4 @@ RoomContainer.propTypes = {
bedroom: PropTypes.number
};

export default RoomContainer;
export default React.memo(RoomContainer);
2 changes: 1 addition & 1 deletion front/src/components/Room/RoomPresenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ const RoomPresenter = ({
);
};

export default RoomPresenter;
export default React.memo(RoomPresenter);
2 changes: 1 addition & 1 deletion front/src/routes/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ const Rooms = () => {
);
};

export default Rooms;
export default React.memo(Rooms);
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"dev": "nodemon --exec babel-node src/server.js --delay 2",
"build": "babel src --out-dir build",
"postbuild": "copy ./src/api/**/*.graphql ./build/api",
"postbuild": "copy -R ./src/api/**/*.graphql ./build/api",
"start": "NODE_ENV=production yarn run build && pm2 start build/server.js"
},
"dependencies": {
Expand Down
6 changes: 5 additions & 1 deletion server/src/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ export const naverLoginCallback = async (
export const createToken = (req, res) => {
const token = generateToken({ id: req.user.id });
res.cookie('token', token);
res.redirect('http://localhost:3000');
const address =
process.env.NODE_ENV === 'production'
? `http://${process.env.DB_PORT}`
: 'http://localhost:3000';
res.redirect(address);
};
4 changes: 2 additions & 2 deletions server/src/seeders/20191015040253-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
up: async (queryInterface, Sequelize) => {
const saltRounds = 10;
return queryInterface.bulkInsert(
'Users',
'users',
[
{
name: 'KyuKyu',
Expand All @@ -23,6 +23,6 @@ export default {
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Users', null, {});
return queryInterface.bulkDelete('users', null, {});
}
};
4 changes: 2 additions & 2 deletions server/src/seeders/20191015041707-room.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default {
row = { ...row, price: row.price * 1000 };
return row;
});
return queryInterface.bulkInsert('Rooms', jsonArray, {});
return queryInterface.bulkInsert('rooms', jsonArray, {});
},

down: (queryInterface, Sequelize) => {
return queryInterface.bulkDelete('Rooms', null, {});
return queryInterface.bulkDelete('rooms', null, {});
}
};

Expand Down

0 comments on commit 5265035

Please sign in to comment.