This is a code base especially created to illustrate a talk about React Fiber.
The talk and the code were done by @mfrachet, @fberthelot and myself.
You can find the "target" code in the master
branch. We try to keep it updated with each React releases.
The livecoding
branch is where the code is changed to an "older" style in order to be improved with last React features.
This chapter is about to describe and keep a script of the livecoding changes in order to update the code from the livecoding
branch to the master
.
Focusing on the src/components/arena/choice/pokemon-input.js
file. It's a typical component used in a loop but which contains two children which has no need of a parent.
- Replace the unused div by an array.
- Replace the array by a
Fragment
. - Replace the
Fragment
with the new notation<>
.
The subject is to generate an error in the Title component.
- In
src/components/design-system/title.js
create anErrorTitle
component which throw an error. Replace the default export to the new component. - http://localhost:3000/arena/choice should fail
- Create from scratch or describe
src/components/design-system/errors.js
. - Use
ErrorHandler
insrc/index.js
around the title. Beware of create-react-app overlay, you can't disable it but you can remove it with escape. - Add
ErrorHandler
insrc/components/arena/choice/choice.js
. Error does not crash the whole app now ! - Use back the normal
Title
component to get the app works again.
Suspense by itself can't be illustrated so, step 3 is more like a placeholder.
We will load the chart library called Victory
lazily.
- In
src/components/stats/stats.js
, add imports forSuspense
andlazy
. - Load the
Chart
component withlazy
and the newimport()
syntax. Add a timeout to the loading function to be able to see the loader. - Surround the
Chart
component withSuspense
, useLoader
as fallback.
We're getting back at the choice page. We will seek to load the data with the new cache API.
- In
src/components/arena/choice/form.js
, remove thecomponentDidMount
method from the component and the pokemon property from the state. - import
createResource
from thereact-cache
local copy insrc/vendor/react-cache.development.js
. Instanciate a resource using the existingfetchApi
function. - In the render method, use the
read
method of the resource to get the data. Use this data in place of the one from the state. - In the
src/components/arena/choice/choice.js
, surround theForm
component with aSuspense
component.
No livecoding, just a démo. Go the stat page.
- Present the debug tool.
- Present the
src/index.js
with the two ways to start React. - Present the
src/components/stats/chart.js
with the two ways to update the state. - Activate the CPU throtling feature of Chrome Dev Tools (Performance tab).
- In standard mode, the rendering freeze clickly and several numbers are lost.
- In concurrent mode, the rendering is slow but no number is lost
Once again, back to the choice page.
- Start a new function component
FormHook
under the other one. Copy the content of therender
method in the function body. - Import the
useState
hook fromreact
. - Use the
useState
hook to createfirst
andsecond
states variable. - Define
handleFirst
andhandleSecond
handlers which set the respective state. - Define the
handleSubmit
handler with the body of the previous - Replace all pointers removing all
this
... Tada :)
The very heart of the hooks are custom hooks. Let's factorize the logic.
- Create a custom hooks factoring state and handler named
useField
.
No livecoding, just looking at the code of the battle.
- Present the
useEffect
hook used to start the fight. - Present the
useReducer
hook to handle the battle logic. - Present the whole code of the component containing any logic code.
- Present the data section.
- Present the logic section, doesn't it look at Redux? Still there is no Redux here! Beware, this reducer is limited to the component state, it's not a global state like Redux (tips: but there is a
useContext
hook).