Skip to content

Commit

Permalink
Merge pull request #23 from zigsong/main
Browse files Browse the repository at this point in the history
[2단계 - Virtual DOM 만들기] 지그(송지은) 미션 제출합니다.
  • Loading branch information
yujo11 authored Oct 31, 2021
2 parents 4b8e57b + 5a15967 commit 8e12756
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
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);
};

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

0 comments on commit 8e12756

Please sign in to comment.