-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish exercises and in-code instructions
- Loading branch information
1 parent
ca7c48d
commit 7dca270
Showing
15 changed files
with
383 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,53 @@ | ||
import { createRoot } from 'react-dom/client' | ||
|
||
// 🐨 create two Symbols for the phase: "INITIALIZATION" and "UPDATE" | ||
// 💯 as extra credit, give them a descriptive name | ||
|
||
// 🦺 create a type called Phase which is the typeof INITIALIZATION | typeof UPDATE | ||
|
||
// 🐨 create a variable called phase of type Phase and set it to INITIALIZATION | ||
|
||
let state: any, setState: any | ||
|
||
function useState<State>(initialState: State) { | ||
let state = initialState | ||
const setState = (newState: State) => (state = newState) | ||
return [state, setState] as const | ||
// 🐨 change this to check whether the phase is INITIALIZATION | ||
if (state === undefined) { | ||
state = initialState | ||
setState = (newState: State) => { | ||
state = newState | ||
// 🐨 pass the UPDATE phase to render here | ||
render() | ||
} | ||
} | ||
return [state, setState] as [State, (newState: State) => void] | ||
} | ||
|
||
function Counter() { | ||
const [count, setCount] = useState(0) | ||
const increment = () => setCount(count + 1) | ||
|
||
const [enabled, setEnabled] = useState(true) | ||
const toggle = () => setEnabled(!enabled) | ||
|
||
return ( | ||
<div className="counter"> | ||
<button onClick={increment}>{count}</button> | ||
<button onClick={toggle}>{enabled ? 'Disable' : 'Enable'}</button> | ||
<button disabled={!enabled} onClick={increment}> | ||
{count} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
const appRoot = createRoot(rootEl) | ||
appRoot.render(<Counter />) | ||
|
||
// 🐨 accept a newPhase argument | ||
function render() { | ||
// 🐨 assign the phase to the newPhase | ||
appRoot.render(<Counter />) | ||
} | ||
|
||
// 🐨 call this with the INITIALIZATION phase | ||
render() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,47 @@ | ||
import { createRoot } from 'react-dom/client' | ||
|
||
const INITIALIZATION = Symbol('phase.initialization') | ||
const UPDATE = Symbol('phase.update') | ||
type Phase = typeof INITIALIZATION | typeof UPDATE | ||
let phase: Phase | ||
|
||
let state: any, setState: any | ||
|
||
function useState<State>(initialState: State) { | ||
let state = initialState | ||
const setState = (newState: State) => (state = newState) | ||
return [state, setState] as const | ||
if (phase === INITIALIZATION) { | ||
state = initialState | ||
setState = (newState: State) => { | ||
state = newState | ||
render(UPDATE) | ||
} | ||
} | ||
return [state, setState] as [State, (newState: State) => void] | ||
} | ||
|
||
function Counter() { | ||
const [count, setCount] = useState(0) | ||
const increment = () => setCount(count + 1) | ||
|
||
const [enabled, setEnabled] = useState(true) | ||
const toggle = () => setEnabled(!enabled) | ||
|
||
return ( | ||
<div className="counter"> | ||
<button onClick={increment}>{count}</button> | ||
<button onClick={toggle}>{enabled ? 'Disable' : 'Enable'}</button> | ||
<button disabled={!enabled} onClick={increment}> | ||
{count} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
const appRoot = createRoot(rootEl) | ||
appRoot.render(<Counter />) | ||
|
||
function render(newPhase: Phase) { | ||
phase = newPhase | ||
appRoot.render(<Counter />) | ||
} | ||
|
||
render(INITIALIZATION) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,57 @@ | ||
import { createRoot } from 'react-dom/client' | ||
|
||
const INITIALIZATION = Symbol('phase.initialization') | ||
const UPDATE = Symbol('phase.update') | ||
type Phase = typeof INITIALIZATION | typeof UPDATE | ||
let phase: Phase | ||
// 🐨 make a hookIndex variable here that starts at 0 | ||
// 🐨 make a variable called "states" which is an array of arrays (one for each | ||
// return value of a useState call) | ||
|
||
// 💣 delete these variable declarations | ||
let state: any, setState: any | ||
|
||
function useState<State>(initialState: State) { | ||
let state = initialState | ||
const setState = (newState: State) => (state = newState) | ||
return [state, setState] as const | ||
// 🐨 create a variable called "id" and assign it to "hookIndex++" | ||
if (phase === INITIALIZATION) { | ||
// 🐨 assign states[id] to an array with the initialState and the setState function | ||
// rather than assigning the values to the old variables | ||
state = initialState | ||
setState = (newState: State) => { | ||
// 🐨 instead of reassigning the variable state to the newState, update states[id][0] to it. | ||
state = newState | ||
render(UPDATE) | ||
} | ||
} | ||
// 🐨 return the value at states[id] instead of the old variables | ||
return [state, setState] as [State, (newState: State) => void] | ||
} | ||
|
||
function Counter() { | ||
const [count, setCount] = useState(0) | ||
const increment = () => setCount(count + 1) | ||
|
||
const [enabled, setEnabled] = useState(true) | ||
const toggle = () => setEnabled(!enabled) | ||
|
||
return ( | ||
<div className="counter"> | ||
<button onClick={increment}>{count}</button> | ||
<button onClick={toggle}>{enabled ? 'Disable' : 'Enable'}</button> | ||
<button disabled={!enabled} onClick={increment}> | ||
{count} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
const appRoot = createRoot(rootEl) | ||
appRoot.render(<Counter />) | ||
|
||
function render(newPhase: Phase) { | ||
// 🐨 set the hookIndex to 0 | ||
phase = newPhase | ||
appRoot.render(<Counter />) | ||
} | ||
|
||
render(INITIALIZATION) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,51 @@ | ||
import { createRoot } from 'react-dom/client' | ||
|
||
const INITIALIZATION = Symbol('phase.initialization') | ||
const UPDATE = Symbol('phase.update') | ||
type Phase = typeof INITIALIZATION | typeof UPDATE | ||
let phase: Phase | ||
let hookIndex = 0 | ||
const states: Array<[any, (newState: any) => void]> = [] | ||
|
||
function useState<State>(initialState: State) { | ||
let state = initialState | ||
const setState = (newState: State) => (state = newState) | ||
return [state, setState] as const | ||
const id = hookIndex++ | ||
if (phase === INITIALIZATION) { | ||
states[id] = [ | ||
initialState, | ||
(newState: State) => { | ||
states[id][0] = newState | ||
render(UPDATE) | ||
}, | ||
] | ||
} | ||
return states[id] as [State, (newState: State) => void] | ||
} | ||
|
||
function Counter() { | ||
const [count, setCount] = useState(0) | ||
const increment = () => setCount(count + 1) | ||
|
||
const [enabled, setEnabled] = useState(true) | ||
const toggle = () => setEnabled(!enabled) | ||
|
||
return ( | ||
<div className="counter"> | ||
<button onClick={increment}>{count}</button> | ||
<button onClick={toggle}>{enabled ? 'Disable' : 'Enable'}</button> | ||
<button disabled={!enabled} onClick={increment}> | ||
{count} | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
const appRoot = createRoot(rootEl) | ||
appRoot.render(<Counter />) | ||
|
||
function render(newPhase: Phase) { | ||
hookIndex = 0 | ||
phase = newPhase | ||
appRoot.render(<Counter />) | ||
} | ||
|
||
render(INITIALIZATION) |
Oops, something went wrong.