Themostimportantthingthat I'd liketotalkabouttodayiscustomhooks, whichcustomhomes, reallyjust a functionthatrunsinsideof a componentthatcanrunotherhooksorotherfunctionslikeyoustateused, reduceorwhatever.
Andlastbutnotleast, I thinktheyhighlyencouragerapidadorationforsomeofthereasonsthat I'm gonnatalkabouttoday.
Sothebiggestreasonthat I believethatallthosethingsweretrueisbeatthathooks, Uh, withtheintroductionofcustomhooks, I thinkthatwe'regoingtoseeorhavealreadyseen a returntocomponentshandling, userinterfaceandhookshandlingbusinesslogic.
Andifthisseemslike a nobrainer, you'reaheadofthecurve.
Andifitdoesn't, um, that's what I'd liketotalkabouttodaythroughsomanymigrationsofoldprojects, twohooksgoing 100% hooksandnewGreenfieldprojectswherewasabletoexperimentwithnewpatternsaroundcustomhooksandsee, youknow, howfarcanwepushthelimitsofthisnew A.
P I I'vediscovered a coupleofusecasesthathavereallysparkednewinterestsforme.
Withthishooks A p I, ummostofthemaroundbuildingportableyou, I utilities.
Ah, fewofthem.
Well, actually a lotofthemaroundManagingGlobalStateandServerSTandalsoencapsulatingbusinesslogicforourapplicationsinsideofthesecustomhooks.
And I wanttosharewithyou a coupleoftheseusecasestodaytoshowyouhowcustomhookscanreallychangethewaythatyouthinkaboutwritingyourapplicationsandtobemoreproductive.
Whynotstartoffwithsomethingthatissohotrightnow?
Youcan't ship a napwithoutit, andit's sometimesit's thefirstthingthatweallthinkabout.
Youcanjustsetupsomestateandtogglebetweenwith a functionofsetisdarktotrueorfalse.
It's reallythateasy.
I wantedsomething a littlemoresophisticatedformydarkmouth.
I don't knowifyouguyshaveeverseenmatchmedia, butit's an A p I thatyoucanusetomatchCSSmediaqueriesinJavaScriptanddetectcertainthingsabouttheuser's environment.
Rightherewehave a matchmethat's detectingwhethertheuserprefers a lightor a darktheme, andwecanactuallysetupourstatetostartwith.
That s sothatiftheuserhas a darkerlightningpreferenceontheirdevice, arestatenowstartswiththatpreferenceandtheycantogglebetweenit.
Butwhohasthisenabledontheircomputer?
It's automodeideoand I loveit.
And I thoughtwouldn't itbeamazingifwecouldmakeitsoourlightanddarkthemeswouldactuallyreacttothatautomaticmodeonourdevices, turnsout.
And I don't knowwhythebrowsersdon't makeiteasiertodothis, Natenatively.
But, uh, youknow, I feellike I'm implementingthiseverydayclickingoutsideof a menuorah, motileorwhatever, right?
Andthenyouwanttodosomethingtoit.
I thought, Whatabout?
Youknow, there's not a betterexcusetowrite a customcomponentforthis, and I thoughtwemightaswelljustmadetheassumptionthatthisisgoingtobe a customhookrightoutofthegate.
So I assumethat I couldtake a refcreatedbyuserref, putitontowhateverelement I wanttokeeptrackofandthenhave a handlersomethinglike, youknow, Consul, logging.
Hey, youclickedoutsideofthisthingand I couldpassthosetwothingstothisuse.
Clickoutsidehookanditwouldjustwork.
And I wrotethisactually, before I wrotetheusequickoutsidehooktoseehowreliableitwastodesign a P eyesaroundhooksbeforeevenwritinghim.
Whydo I havetohavethatusecallbackaroundthatfunction?
Umthisprobablyinadvanceedgecase, but I feellikethere's nothinginsideofmyuseclickoutsidehookthatcaresaboutthiscallbackchanging.
Um, and I decidedthatthat's partofthe A p I.
So I wantedtogetridofit.
AndoneoftheeasiestwaystodothatistoAh, insteadofreferencingthecallbackdirectly, wecanusethispatternoftrackingthelatestversionofthatcallbackin a refitself.
Um, nottonamenamesoranything, butthisissomething I wouldtryandavoid.
I don't I don't wannahavetogototheselengthstouseglobalState.
I'm gonnatakeyouon a littlebitof a journeyoffindingglobalstatetoday, andit's goingtobesointrospectiveandenlighteningthatmaybeyou'llwanttoactuallyhandleyourownglobalstate.
Aftertoday, let's startbycreating a globalstore, wecanusereactcontexttopassvaluesdownourreactortree, nothavetopassthemthroughprops.
Wecanstartwith a storeprovider.
That's just, ahwrapperaroundcontextthatwe'llsetupsomestatewith a storewith a storeand a setstorefunctionrighttherethatwecanpassdownthroughourcontextvalue.
Andthenwe'llhave a usestorecustomhookthatjustwrapsaroundthatusecontext.
Andthatwillgetridof a lotofweird, unnecessaryrendersaroundthatscenario.
Let's takeit a stepfurther.
Whatabout a singlestore?
Uh, isprobablynotgoingtoscaleforverylong.
Ifyouhave a lotofglobalstateinyourapplication, youdon't wanteverysinglecomponentupdatingwhenthestatethatyou'reconsumingupdatesthatglobalstatesowecantakeit a stepfurtherwithmultiplestores.
Wecantakeallofthatlogicthatwehadtocreateourglobalstoreandjustwrapitinto a function.
Andwecancreateasmanyoftheseglobalstoresaswewantbypassingthemin a producerandinitialstate.
Soifwewantedtodostore, wejustpassit.
Buttodoreducergivesusbacktheproviderand a coupleofhookstoconsumeit, andwe'reofftotheraces.
Wecomeintoourapp.
Weprovideourtraducedtotheentireupandyouknowwhat I wantsomeglobalstatetoah, manage.
Allofmymenusebeingopenandstufflikethat's all.
Make a layoutstoreaswell, and I'llwrapthataroundmyappandtheninsideofourcomponent, likeourmenuthatwewantedtoclickoutsideandhaveit, youknow, changearound.
Youknow, I sayservicestatebecause I believethatitistrulydifferentfromglobalstate, butitisthesameinspirit.
ServerSTintroducessomeinterestingchallengesaroundmovingfromsynchronousapeyesto a synchronousapeyes.
We'renolongerinteractingwith a storeor a dispatcherwe'reinteractingwith, youknow, fetchingassetsandsendingmutationsorqueriestotheserverandexpectingthemtodothingsrightandalongAlongwiththat, we'regoingtonoticethat a lotofourcomponentswherewehadpreviouslywiredupthingslikedispatchinthestore, thatkindofbecomes a littlebitof a questionmarknow, wherewegettingthosethings?
Wecandowhateverwewantaslongaswe'rereturning a functionthatthatthecomponentcancallanditisloadinganairstatesowecoulduseourinmemorystoreagainandjusthookitrightuptoourtwo.
There's a librarycreatedcalledReactQuerythathelpswithscenarioslikethis.
Youcanusereactquerytogiveit a functionthatreturns a promiseit'llhandlecashingandinvalidationandmakesurethatyou'reonlyyouknowyou'renotduplicatingtwoDUIsacrossyourentireAP, evenifyouuseitmultipletimes.
I mean, you'llnoticethatthereallycoolpartisthatourreturnsignatureisexactlythesame.
Allmycomponentsarecommunicatingwithcustomhookstohandlemostoftheirbusinesslogic, whicharethenreachingouttopossiblyevenmorecustomhooksorexternally, P eyesorwhatever.