Skip to content

Commit 3bd5572

Browse files
author
Gerard Moreno-Torres Bertran
committed
first commit
1 parent 3dc8f81 commit 3bd5572

File tree

5 files changed

+162
-12
lines changed

5 files changed

+162
-12
lines changed

package-lock.json

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
2-
"name": "reactjson",
2+
"name": "react-json",
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
66
"react": "^16.10.2",
77
"react-dom": "^16.10.2",
8+
"react-reconciler": "^0.22.2",
89
"react-scripts": "3.2.0"
910
},
1011
"scripts": {

src/ReactJSON.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import Reconciler from "react-reconciler";
2+
3+
const createHostConfig = callback => ({
4+
now: Date.now,
5+
getRootHostContext: () => {
6+
let rootContext = {};
7+
return rootContext;
8+
},
9+
getChildHostContext: (parentContext, fiberType) => {
10+
let context = { type: fiberType };
11+
return context;
12+
},
13+
shouldSetTextContent: () => {
14+
return false;
15+
},
16+
createTextInstance: newText => {
17+
return { type: "text", value: newText };
18+
},
19+
createInstance: (type, newProps) => {
20+
return Object.keys(newProps)
21+
.filter(key => key !== "children")
22+
.reduce(
23+
(acc, key) => {
24+
acc[key] = newProps[key];
25+
return acc;
26+
},
27+
{ type }
28+
);
29+
},
30+
appendChildToContainer: (parent, child) => {
31+
if (!parent.children) {
32+
parent.children = [];
33+
}
34+
35+
parent.children.push(child);
36+
},
37+
finalizeInitialChildren: () => {
38+
return false;
39+
},
40+
prepareForCommit: () => {},
41+
resetAfterCommit: () => {},
42+
commitMount: () => {},
43+
appendInitialChild: (parent, child) => {
44+
if (!parent.children) {
45+
parent.children = [];
46+
}
47+
48+
parent.children.push(child);
49+
},
50+
supportsMutation: true,
51+
removeChildFromContainer: (container, child) => {
52+
if (container.children) {
53+
container.children.splice(container.children.indexOf(child), 1);
54+
}
55+
},
56+
prepareUpdate: () => {},
57+
commitTextUpdate: (textInstance, oldText, newText) => {
58+
textInstance.value = newText;
59+
callback();
60+
},
61+
commitUpdate: () => {
62+
callback();
63+
},
64+
appendChild: (parentInstance, child) => {
65+
parentInstance.children.push(child);
66+
},
67+
removeChild: (parentInstance, child) => {
68+
parentInstance.children.splice(parentInstance.children.indexOf(child), 1);
69+
},
70+
resetTextContent: () => {},
71+
shouldDeprioritizeSubtree: () => {
72+
return false;
73+
},
74+
insertInContainerBefore: (container, child, beforeChild) => {
75+
container.children.splice(
76+
container.children.indexOf(beforeChild) - 1,
77+
0,
78+
child
79+
);
80+
},
81+
insertBefore: (parentInstance, child, beforeChild) => {
82+
parentInstance.children.splice(
83+
parentInstance.children.indexOf(beforeChild) - 1,
84+
0,
85+
child
86+
);
87+
}
88+
});
89+
90+
const ReactJSON = {
91+
mount(element, renderDom, callback) {
92+
const isAsync = false;
93+
const reconcilerInstance = Reconciler(createHostConfig(callback));
94+
const container = reconcilerInstance.createContainer(renderDom, isAsync);
95+
96+
const parentComponent = null;
97+
reconcilerInstance.updateContainer(
98+
element,
99+
container,
100+
parentComponent,
101+
callback
102+
);
103+
}
104+
};
105+
106+
export default ReactJSON;

src/State.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from "react";
2+
3+
const Player = () => {
4+
const [life, setLife] = React.useState(20);
5+
const [mana, setMana] = React.useState(20);
6+
7+
React.useEffect(() => {
8+
const interval = setInterval(() => {
9+
setLife(x => x - 1);
10+
setMana(x => x - 1);
11+
}, 100);
12+
13+
return () => clearInterval(interval);
14+
}, []);
15+
16+
return (
17+
<player>
18+
<life>{life}</life>
19+
<mana>{mana}</mana>
20+
{life < 10 && life > 0 ? "Running out of mana" : null}
21+
</player>
22+
);
23+
};
24+
25+
const State = () => {
26+
return (
27+
<state>
28+
<Player active={true} />
29+
<Player active={false} />
30+
</state>
31+
);
32+
};
33+
34+
export default State;

src/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
1+
import React from "react";
62

7-
ReactDOM.render(<App />, document.getElementById('root'));
3+
import ReactJSON from "./ReactJSON";
4+
import State from "./State";
85

9-
// If you want your app to work offline and load faster, you can change
10-
// unregister() to register() below. Note this comes with some pitfalls.
11-
// Learn more about service workers: https://bit.ly/CRA-PWA
12-
serviceWorker.unregister();
6+
const root = {};
7+
8+
ReactJSON.mount(<State />, root, () => {
9+
console.log(root);
10+
});

0 commit comments

Comments
 (0)