Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2단계 - Virtual DOM 만들기] 지그(송지은) 미션 제출합니다. #23

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/components/Counter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import Zig from '../lib/zig-react';

const Counter = () => {
const [getCount, setCount] = Zig.useState(0);
const [initialCount, setCount] = Zig.useState(0);

const count = new Proxy(initialCount, {
get(target, prop) {
try {
let value = target[prop];

return typeof target.value === 'function' ? value.call(target) : target.value;
} catch (error) {
console.error(`Proxy get Error: ${error}`);
}
},
});

const decrease = () => {
setCount(getCount() - 1);
setCount(count.value - 1);
};

const increase = () => {
setCount(getCount() + 1);
setCount(count.value + 1);
Comment on lines +4 to +23
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프록시를 사용한다면 Zig.useState()를 아예 제거하고 Proxy 내부에 set 핸들러를 정의해서 사용하면 어떨까 하는 생각이 드네용

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 그것도 좋은 생각이에요!!
이번 미션에서는 그냥 기존 React의 useState와 비슷하게 구현하고 싶어서 (setCount()의 형태로 쓰고 싶어서 ㅎㅅㅎ) 이렇게 두었습니다!!

의견 제시 고마와용 🤩

};

const reset = () => {
Expand All @@ -19,7 +31,7 @@ const Counter = () => {
Zig.createElement(
'div',
{ className: 'container' },
Zig.createElement('span', { className: 'count' }, getCount()),
Zig.createElement('span', { className: 'count' }, count.value),
Zig.createElement(
'div',
{ className: 'btn-group' },
Expand Down
6 changes: 5 additions & 1 deletion src/lib/zig-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const Zig = (function () {

const getState = () => hooks[_idx] || initialValue;

const state = {
value: getState,
};

const setState = (newValue) => {
hooks[_idx] = newValue;

Expand All @@ -38,7 +42,7 @@ const Zig = (function () {

idx++;

return [getState, setState];
return [state, setState];
};

return { createElement, useState };
Expand Down