print.html 260 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title></title>
  7. <meta name="robots" content="noindex" />
  8. <!-- Custom HTML head -->
  9. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  10. <meta name="description" content="">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <meta name="theme-color" content="#ffffff" />
  13. <link rel="icon" href="favicon.svg">
  14. <link rel="shortcut icon" href="favicon.png">
  15. <link rel="stylesheet" href="css/variables.css">
  16. <link rel="stylesheet" href="css/general.css">
  17. <link rel="stylesheet" href="css/chrome.css">
  18. <link rel="stylesheet" href="css/print.css" media="print">
  19. <!-- Fonts -->
  20. <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
  21. <link rel="stylesheet" href="fonts/fonts.css">
  22. <!-- Highlight.js Stylesheets -->
  23. <link rel="stylesheet" href="highlight.css">
  24. <link rel="stylesheet" href="tomorrow-night.css">
  25. <link rel="stylesheet" href="ayu-highlight.css">
  26. <!-- Custom theme stylesheets -->
  27. <!-- MathJax -->
  28. <script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  29. </head>
  30. <body>
  31. <!-- Provide site root to javascript -->
  32. <script type="text/javascript">
  33. var path_to_root = "";
  34. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  35. </script>
  36. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  37. <script type="text/javascript">
  38. try {
  39. var theme = localStorage.getItem('mdbook-theme');
  40. var sidebar = localStorage.getItem('mdbook-sidebar');
  41. if (theme.startsWith('"') && theme.endsWith('"')) {
  42. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  43. }
  44. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  45. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  46. }
  47. } catch (e) { }
  48. </script>
  49. <!-- Set the theme before any content is loaded, prevents flash -->
  50. <script type="text/javascript">
  51. var theme;
  52. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  53. if (theme === null || theme === undefined) { theme = default_theme; }
  54. var html = document.querySelector('html');
  55. html.classList.remove('no-js')
  56. html.classList.remove('light')
  57. html.classList.add(theme);
  58. html.classList.add('js');
  59. </script>
  60. <!-- Hide / unhide sidebar before it is displayed -->
  61. <script type="text/javascript">
  62. var html = document.querySelector('html');
  63. var sidebar = 'hidden';
  64. if (document.body.clientWidth >= 1080) {
  65. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  66. sidebar = sidebar || 'visible';
  67. }
  68. html.classList.remove('sidebar-visible');
  69. html.classList.add("sidebar-" + sidebar);
  70. </script>
  71. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  72. <div class="sidebar-scrollbox">
  73. <ol class="chapter"><li class="chapter-item expanded affix "><a href="index.html">Introduction</a></li><li class="chapter-item expanded "><a href="getting_started/index.html"><strong aria-hidden="true">1.</strong> Getting Started</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="getting_started/desktop.html"><strong aria-hidden="true">1.1.</strong> Desktop</a></li><li class="chapter-item expanded "><a href="getting_started/web.html"><strong aria-hidden="true">1.2.</strong> Web</a></li><li class="chapter-item expanded "><a href="getting_started/ssr.html"><strong aria-hidden="true">1.3.</strong> Server-Side Rendering</a></li><li class="chapter-item expanded "><a href="getting_started/fullstack.html"><strong aria-hidden="true">1.4.</strong> Fullstack</a></li><li class="chapter-item expanded "><a href="getting_started/liveview.html"><strong aria-hidden="true">1.5.</strong> Liveview</a></li><li class="chapter-item expanded "><a href="getting_started/tui.html"><strong aria-hidden="true">1.6.</strong> Terminal UI</a></li><li class="chapter-item expanded "><a href="getting_started/mobile.html"><strong aria-hidden="true">1.7.</strong> Mobile</a></li><li class="chapter-item expanded "><a href="getting_started/hot_reload.html"><strong aria-hidden="true">1.8.</strong> Hot Reloading</a></li></ol></li><li class="chapter-item expanded "><a href="describing_ui/index.html"><strong aria-hidden="true">2.</strong> Describing the UI</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="describing_ui/special_attributes.html"><strong aria-hidden="true">2.1.</strong> Special Attributes</a></li><li class="chapter-item expanded "><a href="describing_ui/components.html"><strong aria-hidden="true">2.2.</strong> Components</a></li><li class="chapter-item expanded "><a href="describing_ui/component_props.html"><strong aria-hidden="true">2.3.</strong> Props</a></li><li class="chapter-item expanded "><a href="describing_ui/component_children.html"><strong aria-hidden="true">2.4.</strong> Component Children</a></li></ol></li><li class="chapter-item expanded "><a href="interactivity/index.html"><strong aria-hidden="true">3.</strong> Interactivity</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="interactivity/event_handlers.html"><strong aria-hidden="true">3.1.</strong> Event Listeners</a></li><li class="chapter-item expanded "><a href="interactivity/hooks.html"><strong aria-hidden="true">3.2.</strong> Hooks &amp; Component State</a></li><li class="chapter-item expanded "><a href="interactivity/user_input.html"><strong aria-hidden="true">3.3.</strong> User Input</a></li><li class="chapter-item expanded "><a href="interactivity/sharing_state.html"><strong aria-hidden="true">3.4.</strong> Sharing State</a></li><li class="chapter-item expanded "><a href="interactivity/custom_hooks.html"><strong aria-hidden="true">3.5.</strong> Custom Hooks</a></li><li class="chapter-item expanded "><a href="interactivity/dynamic_rendering.html"><strong aria-hidden="true">3.6.</strong> Dynamic Rendering</a></li><li class="chapter-item expanded "><a href="interactivity/router.html"><strong aria-hidden="true">3.7.</strong> Routing</a></li></ol></li><li class="chapter-item expanded "><a href="async/index.html"><strong aria-hidden="true">4.</strong> Async</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="async/use_future.html"><strong aria-hidden="true">4.1.</strong> UseFuture</a></li><li class="chapter-item expanded "><a href="async/use_coroutine.html"><strong aria-hidden="true">4.2.</strong> UseCoroutine</a></li><li class="chapter-item expanded "><a href="async/spawn.html"><strong aria-hidden="true">4.3.</strong> Spawning Futures</a></li></ol></li><li class="chapter-item expanded "><a href="best_practices/index.html"><strong aria-hidden="true">5.</strong> Best Practices</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="best_practices/error_handling.html"><strong aria-hidden="true">5.1.</strong> Error Handling</a></li><li class="chapter-item expanded "><a href="best_practices/antipatterns.html"><strong aria-hidden="true">5.2.</strong> Antipatterns</a></li></ol></li><li class="chapter-item expanded "><a href="publishing/index.html"><strong aria-hidden="true">6.</strong> Publishing</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="publishing/desktop.html"><strong aria-hidden="true">6.1.</strong> Desktop</a></li><li class="chapter-item expanded "><a href="publishing/web.html"><strong aria-hidden="true">6.2.</strong> Web</a></li><li class="spacer"></li></ol></li><li class="chapter-item expanded "><a href="fullstack/index.html"><strong aria-hidden="true">7.</strong> Fullstack</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="fullstack/getting_started.html"><strong aria-hidden="true">7.1.</strong> Getting Started</a></li><li class="chapter-item expanded "><a href="fullstack/server_functions.html"><strong aria-hidden="true">7.2.</strong> Communicating with the Server</a></li><li class="spacer"></li></ol></li><li class="chapter-item expanded "><a href="custom_renderer/index.html"><strong aria-hidden="true">8.</strong> Custom Renderer</a></li><li class="spacer"></li><li class="chapter-item expanded "><a href="contributing/index.html"><strong aria-hidden="true">9.</strong> Contributing</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="contributing/project_structure.html"><strong aria-hidden="true">9.1.</strong> Project Structure</a></li><li class="chapter-item expanded "><a href="contributing/walkthrough_readme.html"><strong aria-hidden="true">9.2.</strong> Walkthrough of Internals</a></li><li class="chapter-item expanded "><a href="contributing/guiding_principles.html"><strong aria-hidden="true">9.3.</strong> Guiding Principles</a></li><li class="chapter-item expanded "><a href="contributing/roadmap.html"><strong aria-hidden="true">9.4.</strong> Roadmap</a></li></ol></li></ol>
  74. </div>
  75. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  76. </nav>
  77. <div id="page-wrapper" class="page-wrapper">
  78. <div class="page">
  79. <div id="menu-bar-hover-placeholder"></div>
  80. <div id="menu-bar" class="menu-bar sticky bordered">
  81. <div class="left-buttons">
  82. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  83. <i class="fa fa-bars"></i>
  84. </button>
  85. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  86. <i class="fa fa-paint-brush"></i>
  87. </button>
  88. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  89. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  90. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  91. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  92. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  93. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  94. </ul>
  95. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  96. <i class="fa fa-search"></i>
  97. </button>
  98. <button id="language-toggle" class="icon-button" type="button" title="Select language" aria-label="Select language" aria-haspopup="true" aria-expanded="false" aria-controls="language-list">
  99. <i class="fa fa-globe"></i>
  100. </button>
  101. <ul id="language-list" class="language-popup" aria-label="Languages" role="menu">
  102. <li role="none"><a href="../en/print.html"><button role="menuitem" class="language" id="light">English</button></a></li>
  103. <li role="none"><a href="../pt-br/print.html"><button role="menuitem" class="language" id="light">Português Brasileiro</button></a></li>
  104. </ul>
  105. </div>
  106. <h1 class="menu-title"></h1>
  107. <div class="right-buttons">
  108. <a href="print.html" title="Print this book" aria-label="Print this book">
  109. <i id="print-button" class="fa fa-print"></i>
  110. </a>
  111. <a href="https://github.com/DioxusLabs/dioxus/edit/master/docs/guide" title="Git repository" aria-label="Git repository">
  112. <i id="git-repository-button" class="fa fa-github"></i>
  113. </a>
  114. </div>
  115. </div>
  116. <div id="search-wrapper" class="hidden">
  117. <form id="searchbar-outer" class="searchbar-outer">
  118. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  119. </form>
  120. <div id="searchresults-outer" class="searchresults-outer hidden">
  121. <div id="searchresults-header" class="searchresults-header"></div>
  122. <ul id="searchresults">
  123. </ul>
  124. </div>
  125. </div>
  126. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  127. <script type="text/javascript">
  128. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  129. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  130. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  131. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  132. });
  133. </script>
  134. <div id="content" class="content">
  135. <main>
  136. <h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
  137. <p><img src="./images/dioxuslogo_full.png" alt="dioxuslogo" /></p>
  138. <p>Dioxus is a portable, performant, and ergonomic framework for building cross-platform user interfaces in Rust. This guide will help you get started with writing Dioxus apps for the Web, Desktop, Mobile, and more.</p>
  139. <pre><pre class="playground"><code class="language-rust edition2018">
  140. <span class="boring">#![allow(unused)]
  141. </span><span class="boring">fn main() {
  142. </span>fn app(cx: Scope) -&gt; Element {
  143. let mut count = use_state(cx, || 0);
  144. cx.render(rsx!(
  145. h1 { &quot;High-Five counter: {count}&quot; }
  146. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  147. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  148. ))
  149. }
  150. <span class="boring">}
  151. </span></code></pre></pre>
  152. <p>Dioxus is heavily inspired by React. If you know React, getting started with Dioxus will be a breeze.</p>
  153. <blockquote>
  154. <p>This guide assumes you already know some <a href="https://www.rust-lang.org/">Rust</a>! If not, we recommend reading <a href="https://doc.rust-lang.org/book/ch01-00-getting-started.html"><em>the book</em></a> to learn Rust first.</p>
  155. </blockquote>
  156. <h2 id="features"><a class="header" href="#features">Features</a></h2>
  157. <ul>
  158. <li>Desktop apps running natively (no Electron!) in less than 10 lines of code.</li>
  159. <li>Incredibly ergonomic and powerful state management.</li>
  160. <li>Comprehensive inline documentation – hover and guides for all HTML elements, listeners, and events.</li>
  161. <li>Extremely memory efficient – 0 global allocations for steady-state components.</li>
  162. <li>Multi-channel asynchronous scheduler for first-class async support.</li>
  163. <li>And more! Read the <a href="https://dioxuslabs.com/blog/introducing-dioxus/">full release post</a>.</li>
  164. </ul>
  165. <h3 id="multiplatform"><a class="header" href="#multiplatform">Multiplatform</a></h3>
  166. <p>Dioxus is a <em>portable</em> toolkit, meaning the Core implementation can run anywhere with no platform-dependent linking. Unlike many other Rust frontend toolkits, Dioxus is not intrinsically linked to WebSys. In fact, every element and event listener can be swapped out at compile time. By default, Dioxus ships with the <code>html</code> feature enabled, but this can be disabled depending on your target renderer.</p>
  167. <p>Right now, we have several 1st-party renderers:</p>
  168. <ul>
  169. <li>WebSys (for WASM): Great support</li>
  170. <li>Tao/Tokio (for Desktop apps): Good support</li>
  171. <li>Tao/Tokio (for Mobile apps): Poor support</li>
  172. <li>SSR (for generating static markup)</li>
  173. <li>TUI/Rink (for terminal-based apps): Experimental</li>
  174. </ul>
  175. <h2 id="stability"><a class="header" href="#stability">Stability</a></h2>
  176. <p>Dioxus has not reached a stable release yet.</p>
  177. <p>Web: Since the web is a fairly mature platform, we expect there to be very little API churn for web-based features.</p>
  178. <p>Desktop: APIs will likely be in flux as we figure out better patterns than our ElectronJS counterpart.</p>
  179. <p>SSR: We don't expect the SSR API to change drastically in the future.</p>
  180. <div style="break-before: page; page-break-before: always;"></div><h1 id="getting-started"><a class="header" href="#getting-started">Getting Started</a></h1>
  181. <p>This section will help you set up your Dioxus project!</p>
  182. <h2 id="prerequisites"><a class="header" href="#prerequisites">Prerequisites</a></h2>
  183. <h3 id="an-editor"><a class="header" href="#an-editor">An Editor</a></h3>
  184. <p>Dioxus integrates very well with the <a href="https://rust-analyzer.github.io">Rust-Analyzer LSP plugin</a> which will provide appropriate syntax highlighting, code navigation, folding, and more.</p>
  185. <h3 id="rust"><a class="header" href="#rust">Rust</a></h3>
  186. <p>Head over to <a href="http://rust-lang.org">https://rust-lang.org</a> and install the Rust compiler.</p>
  187. <p>We strongly recommend going through the <a href="https://doc.rust-lang.org/book/ch01-00-getting-started.html">official Rust book</a> <em>completely</em>. However, we hope that a Dioxus app can serve as a great first Rust project. With Dioxus, you'll learn about:</p>
  188. <ul>
  189. <li>Error handling</li>
  190. <li>Structs, Functions, Enums</li>
  191. <li>Closures</li>
  192. <li>Macros</li>
  193. </ul>
  194. <p>We've put a lot of care into making Dioxus syntax familiar and easy to understand, so you won't need deep knowledge of async, lifetimes, or smart pointers until you start building complex Dioxus apps.</p>
  195. <h2 id="setup-guides"><a class="header" href="#setup-guides">Setup Guides</a></h2>
  196. <p>Dioxus supports multiple platforms. Choose the platform you want to target below to get platform-specific setup instructions:</p>
  197. <ul>
  198. <li><a href="getting_started/web.html">Web</a>: runs in the browser through WebAssembly</li>
  199. <li><a href="getting_started/ssr.html">Server Side Rendering</a>: renders to HTML text on the server</li>
  200. <li><a href="getting_started/liveview.html">Liveview</a>: runs on the server, renders in the browser using WebSockets</li>
  201. <li><a href="getting_started/desktop.html">Desktop</a>: runs in a web view on desktop</li>
  202. <li><a href="getting_started/mobile.html">Mobile</a>: runs in a web view on mobile</li>
  203. <li><a href="getting_started/tui.html">Terminal UI</a>: renders text-based graphics in the terminal</li>
  204. </ul>
  205. <div style="break-before: page; page-break-before: always;"></div><h1 id="desktop-overview"><a class="header" href="#desktop-overview">Desktop Overview</a></h1>
  206. <p>Build a standalone native desktop app that looks and feels the same across operating systems.</p>
  207. <p>Apps built with Dioxus are typically &lt;5mb in size and use existing system resources, so they won't hog extreme amounts of RAM or memory.</p>
  208. <p>Examples:</p>
  209. <ul>
  210. <li><a href="https://github.com/DioxusLabs/example-projects/blob/master/file-explorer">File Explorer</a></li>
  211. <li><a href="https://github.com/DioxusLabs/example-projects/blob/master/wifi-scanner">WiFi Scanner</a></li>
  212. </ul>
  213. <p><a href="https://github.com/DioxusLabs/example-projects/tree/master/file-explorer"><img src="https://raw.githubusercontent.com/DioxusLabs/example-projects/master/file-explorer/image.png" alt="File ExplorerExample" /></a></p>
  214. <h2 id="support"><a class="header" href="#support">Support</a></h2>
  215. <p>The desktop is a powerful target for Dioxus but is currently limited in capability when compared to the Web platform. Currently, desktop apps are rendered with the platform's WebView library, but your Rust code is running natively on a native thread. This means that browser APIs are <em>not</em> available, so rendering WebGL, Canvas, etc is not as easy as the Web. However, native system APIs <em>are</em> accessible, so streaming, WebSockets, filesystem, etc are all viable APIs. In the future, we plan to move to a custom web renderer-based DOM renderer with WGPU integrations.</p>
  216. <p>Dioxus Desktop is built off <a href="https://tauri.app/">Tauri</a>. Right now there aren't any Dioxus abstractions over the menubar, handling, etc, so you'll want to leverage Tauri – mostly <a href="http://github.com/tauri-apps/wry/">Wry</a> and <a href="http://github.com/tauri-apps/tao">Tao</a>) directly.</p>
  217. <h1 id="getting-started-1"><a class="header" href="#getting-started-1">Getting started</a></h1>
  218. <h2 id="platform-specific-dependencies"><a class="header" href="#platform-specific-dependencies">Platform-Specific Dependencies</a></h2>
  219. <p>Dioxus desktop renders through a web view. Depending on your platform, you might need to install some dependancies.</p>
  220. <h3 id="windows"><a class="header" href="#windows">Windows</a></h3>
  221. <p>Windows Desktop apps depend on WebView2 – a library that should be installed in all modern Windows distributions. If you have Edge installed, then Dioxus will work fine. If you <em>don't</em> have Webview2, <a href="https://developer.microsoft.com/en-us/microsoft-edge/webview2/">then you can install it through Microsoft</a>. MS provides 3 options:</p>
  222. <ol>
  223. <li>A tiny &quot;evergreen&quot; <em>bootstrapper</em> that fetches an installer from Microsoft's CDN</li>
  224. <li>A tiny <em>installer</em> that fetches Webview2 from Microsoft's CDN</li>
  225. <li>A statically linked version of Webview2 in your final binary for offline users</li>
  226. </ol>
  227. <p>For development purposes, use Option 1.</p>
  228. <h3 id="linux"><a class="header" href="#linux">Linux</a></h3>
  229. <p>Webview Linux apps require WebkitGtk. When distributing, this can be part of your dependency tree in your <code>.rpm</code> or <code>.deb</code>. However, likely, your users will already have WebkitGtk.</p>
  230. <pre><code class="language-bash">sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev
  231. </code></pre>
  232. <p>When using Debian/bullseye <code>libappindicator3-dev</code> is no longer available but replaced by <code>libayatana-appindicator3-dev</code>.</p>
  233. <pre><code class="language-bash"># on Debian/bullseye use:
  234. sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev
  235. </code></pre>
  236. <p>If you run into issues, make sure you have all the basics installed, as outlined in the <a href="https://tauri.studio/v1/guides/getting-started/prerequisites#setting-up-linux">Tauri docs</a>.</p>
  237. <h3 id="macos"><a class="header" href="#macos">MacOS</a></h3>
  238. <p>Currently – everything for macOS is built right in! However, you might run into an issue if you're using nightly Rust due to some permissions issues in our Tao dependency (which have been resolved but not published).</p>
  239. <h2 id="creating-a-project"><a class="header" href="#creating-a-project">Creating a Project</a></h2>
  240. <p>Create a new crate:</p>
  241. <pre><code class="language-shell">cargo new --bin demo
  242. cd demo
  243. </code></pre>
  244. <p>Add Dioxus and the desktop renderer as dependencies (this will edit your <code>Cargo.toml</code>):</p>
  245. <pre><code class="language-shell">cargo add dioxus
  246. cargo add dioxus-desktop
  247. </code></pre>
  248. <p>Edit your <code>main.rs</code>:</p>
  249. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case)]
  250. // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
  251. use dioxus::prelude::*;
  252. fn main() {
  253. // launch the dioxus app in a webview
  254. dioxus_desktop::launch(App);
  255. }
  256. // define a component that renders a div with the text &quot;Hello, world!&quot;
  257. fn App(cx: Scope) -&gt; Element {
  258. cx.render(rsx! {
  259. div {
  260. &quot;Hello, world!&quot;
  261. }
  262. })
  263. }
  264. </code></pre></pre>
  265. <div style="break-before: page; page-break-before: always;"></div><h1 id="web"><a class="header" href="#web">Web</a></h1>
  266. <p>Build single-page applications that run in the browser with Dioxus. To run on the Web, your app must be compiled to WebAssembly and depend on the <code>dioxus</code> and <code>dioxus-web</code> crates.</p>
  267. <p>A build of Dioxus for the web will be roughly equivalent to the size of a React build (70kb vs 65kb) but it will load significantly faster because <a href="https://hacks.mozilla.org/2018/01/making-webassembly-even-faster-firefoxs-new-streaming-and-tiering-compiler/">WebAssembly can be compiled as it is streamed</a>.</p>
  268. <p>Examples:</p>
  269. <ul>
  270. <li><a href="https://github.com/DioxusLabs/example-projects/tree/master/todomvc">TodoMVC</a></li>
  271. <li><a href="https://github.com/DioxusLabs/example-projects/tree/master/ecommerce-site">ECommerce</a></li>
  272. </ul>
  273. <p><a href="https://github.com/DioxusLabs/example-projects/blob/master/todomvc"><img src="https://github.com/DioxusLabs/example-projects/raw/master/todomvc/example.png" alt="TodoMVC example" /></a></p>
  274. <blockquote>
  275. <p>Note: Because of the limitations of Wasm, <a href="https://rustwasm.github.io/docs/book/reference/which-crates-work-with-wasm.html">not every crate will work</a> with your web apps, so you'll need to make sure that your crates work without native system calls (timers, IO, etc).</p>
  276. </blockquote>
  277. <h2 id="support-1"><a class="header" href="#support-1">Support</a></h2>
  278. <p>The Web is the best-supported target platform for Dioxus.</p>
  279. <ul>
  280. <li>Because your app will be compiled to WASM you have access to browser APIs through <a href="https://rustwasm.github.io/docs/wasm-bindgen/introduction.html">wasm-bingen</a>.</li>
  281. <li>Dioxus provides hydration to resume apps that are rendered on the server. See the <a href="getting_started/fullstack.html">fullstack</a> getting started guide for more information.</li>
  282. </ul>
  283. <h2 id="tooling"><a class="header" href="#tooling">Tooling</a></h2>
  284. <p>To develop your Dioxus app for the web, you'll need a tool to build and serve your assets. We recommend using <a href="https://github.com/DioxusLabs/cli">dioxus-cli</a> which includes a build system, Wasm optimization, a dev server, and support hot reloading:</p>
  285. <pre><code class="language-shell">cargo install dioxus-cli
  286. </code></pre>
  287. <p>Make sure the <code>wasm32-unknown-unknown</code> target for rust is installed:</p>
  288. <pre><code class="language-shell">rustup target add wasm32-unknown-unknown
  289. </code></pre>
  290. <h2 id="creating-a-project-1"><a class="header" href="#creating-a-project-1">Creating a Project</a></h2>
  291. <p>Create a new crate:</p>
  292. <pre><code class="language-shell">cargo new --bin demo
  293. cd demo
  294. </code></pre>
  295. <p>Add Dioxus and the web renderer as dependencies (this will edit your <code>Cargo.toml</code>):</p>
  296. <pre><code class="language-bash">cargo add dioxus
  297. cargo add dioxus-web
  298. </code></pre>
  299. <p>Edit your <code>main.rs</code>:</p>
  300. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case)]
  301. // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
  302. use dioxus::prelude::*;
  303. fn main() {
  304. // launch the web app
  305. dioxus_web::launch(App);
  306. }
  307. // create a component that renders a div with the text &quot;Hello, world!&quot;
  308. fn App(cx: Scope) -&gt; Element {
  309. cx.render(rsx! {
  310. div {
  311. &quot;Hello, world!&quot;
  312. }
  313. })
  314. }
  315. </code></pre></pre>
  316. <p>And to serve our app:</p>
  317. <pre><code class="language-bash">dioxus serve
  318. </code></pre>
  319. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-side-rendering"><a class="header" href="#server-side-rendering">Server-Side Rendering</a></h1>
  320. <p>For lower-level control over the rendering process, you can use the <code>dioxus-ssr</code> crate directly. This can be useful when integrating with a web framework that <code>dioxus-server</code> does not support, or pre-rendering pages.</p>
  321. <h2 id="setup"><a class="header" href="#setup">Setup</a></h2>
  322. <p>For this guide, we're going to show how to use Dioxus SSR with <a href="https://docs.rs/axum/latest/axum/">Axum</a>.</p>
  323. <p>Make sure you have Rust and Cargo installed, and then create a new project:</p>
  324. <pre><code class="language-shell">cargo new --bin demo
  325. cd demo
  326. </code></pre>
  327. <p>Add Dioxus and the ssr renderer as dependencies:</p>
  328. <pre><code class="language-shell">cargo add dioxus
  329. cargo add dioxus-ssr
  330. </code></pre>
  331. <p>Next, add all the Axum dependencies. This will be different if you're using a different Web Framework</p>
  332. <pre><code>cargo add tokio --features full
  333. cargo add axum
  334. </code></pre>
  335. <p>Your dependencies should look roughly like this:</p>
  336. <pre><code class="language-toml">[dependencies]
  337. axum = &quot;0.4.5&quot;
  338. dioxus = { version = &quot;*&quot; }
  339. dioxus-ssr = { version = &quot;*&quot; }
  340. tokio = { version = &quot;1.15.0&quot;, features = [&quot;full&quot;] }
  341. </code></pre>
  342. <p>Now, set up your Axum app to respond on an endpoint.</p>
  343. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case)]
  344. use axum::{response::Html, routing::get, Router};
  345. // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
  346. use dioxus::prelude::*;
  347. #[tokio::main]
  348. async fn main() {
  349. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 3000));
  350. println!(&quot;listening on http://{}&quot;, addr);
  351. axum::Server::bind(&amp;addr)
  352. .serve(
  353. Router::new()
  354. .route(&quot;/&quot;, get(app_endpoint))
  355. .into_make_service(),
  356. )
  357. .await
  358. .unwrap();
  359. }
  360. </code></pre></pre>
  361. <p>And then add our endpoint. We can either render <code>rsx!</code> directly:</p>
  362. <pre><pre class="playground"><code class="language-rust edition2018">
  363. <span class="boring">#![allow(unused)]
  364. </span><span class="boring">fn main() {
  365. </span>async fn app_endpoint() -&gt; Html&lt;String&gt; {
  366. // render the rsx! macro to HTML
  367. Html(dioxus_ssr::render_lazy(rsx! {
  368. div { &quot;hello world!&quot; }
  369. }))
  370. }
  371. <span class="boring">}
  372. </span></code></pre></pre>
  373. <p>Or we can render VirtualDoms.</p>
  374. <pre><pre class="playground"><code class="language-rust edition2018">
  375. <span class="boring">#![allow(unused)]
  376. </span><span class="boring">fn main() {
  377. </span>async fn second_app_endpoint() -&gt; Html&lt;String&gt; {
  378. // create a component that renders a div with the text &quot;hello world&quot;
  379. fn app(cx: Scope) -&gt; Element {
  380. cx.render(rsx!(div { &quot;hello world&quot; }))
  381. }
  382. // create a VirtualDom with the app component
  383. let mut app = VirtualDom::new(app);
  384. // rebuild the VirtualDom before rendering
  385. let _ = app.rebuild();
  386. // render the VirtualDom to HTML
  387. Html(dioxus_ssr::render(&amp;app))
  388. }
  389. <span class="boring">}
  390. </span></code></pre></pre>
  391. <p>And then add our app component:</p>
  392. <pre><pre class="playground"><code class="language-rust edition2018">
  393. <span class="boring">#![allow(unused)]
  394. </span><span class="boring">fn main() {
  395. </span>// define a component that renders a div with the text &quot;Hello, world!&quot;
  396. fn App(cx: Scope) -&gt; Element {
  397. cx.render(rsx! {
  398. div {
  399. &quot;Hello, world!&quot;
  400. }
  401. })
  402. }
  403. <span class="boring">}
  404. </span></code></pre></pre>
  405. <p>And that's it!</p>
  406. <h2 id="multithreaded-support"><a class="header" href="#multithreaded-support">Multithreaded Support</a></h2>
  407. <p>The Dioxus VirtualDom, sadly, is not currently <code>Send</code>. Internally, we use quite a bit of interior mutability which is not thread-safe.
  408. When working with web frameworks that require <code>Send</code>, it is possible to render a VirtualDom immediately to a String – but you cannot hold the VirtualDom across an await point. For retained-state SSR (essentially LiveView), you'll need to spawn a VirtualDom on its own thread and communicate with it via channels or create a pool of VirtualDoms.
  409. You might notice that you cannot hold the VirtualDom across an await point. Because Dioxus is currently not ThreadSafe, it <em>must</em> remain on the thread it started. We are working on loosening this requirement.</p>
  410. <div style="break-before: page; page-break-before: always;"></div><h1 id="fullstack"><a class="header" href="#fullstack">Fullstack</a></h1>
  411. <div style="break-before: page; page-break-before: always;"></div><h1 id="liveview"><a class="header" href="#liveview">Liveview</a></h1>
  412. <p>Liveview allows apps to <em>run</em> on the server and <em>render</em> in the browser. It uses WebSockets to communicate between the server and the browser.</p>
  413. <p>Examples:</p>
  414. <ul>
  415. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/liveview/examples/axum.rs">Axum Example</a></li>
  416. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/liveview/examples/salvo.rs">Salvo Example</a></li>
  417. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/liveview/examples/warp.rs">Warp Example</a></li>
  418. </ul>
  419. <h2 id="support-2"><a class="header" href="#support-2">Support</a></h2>
  420. <p>Liveview is currently limited in capability when compared to the Web platform. Liveview apps run on the server in a native thread. This means that browser APIs are not available, so rendering WebGL, Canvas, etc is not as easy as the Web. However, native system APIs are accessible, so streaming, WebSockets, filesystem, etc are all viable APIs.</p>
  421. <h2 id="setup-1"><a class="header" href="#setup-1">Setup</a></h2>
  422. <p>For this guide, we're going to show how to use Dioxus Liveview with <a href="https://docs.rs/axum/latest/axum/">Axum</a>.</p>
  423. <p>Make sure you have Rust and Cargo installed, and then create a new project:</p>
  424. <pre><code class="language-shell">cargo new --bin demo
  425. cd app
  426. </code></pre>
  427. <p>Add Dioxus and the liveview renderer with the Axum feature as dependencies:</p>
  428. <pre><code class="language-shell">cargo add dioxus
  429. cargo add dioxus-liveview --features axum
  430. </code></pre>
  431. <p>Next, add all the Axum dependencies. This will be different if you're using a different Web Framework</p>
  432. <pre><code>cargo add tokio --features full
  433. cargo add axum
  434. </code></pre>
  435. <p>Your dependencies should look roughly like this:</p>
  436. <pre><code class="language-toml">[dependencies]
  437. axum = &quot;0.4.5&quot;
  438. dioxus = { version = &quot;*&quot; }
  439. dioxus-liveview = { version = &quot;*&quot;, features = [&quot;axum&quot;] }
  440. tokio = { version = &quot;1.15.0&quot;, features = [&quot;full&quot;] }
  441. </code></pre>
  442. <p>Now, set up your Axum app to respond on an endpoint.</p>
  443. <pre><pre class="playground"><code class="language-rust edition2018">#[tokio::main]
  444. async fn main() {
  445. let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
  446. let view = dioxus_liveview::LiveViewPool::new();
  447. let app = Router::new()
  448. // The root route contains the glue code to connect to the WebSocket
  449. .route(
  450. &quot;/&quot;,
  451. get(move || async move {
  452. Html(format!(
  453. r#&quot;
  454. &lt;!DOCTYPE html&gt;
  455. &lt;html&gt;
  456. &lt;head&gt; &lt;title&gt;Dioxus LiveView with Axum&lt;/title&gt; &lt;/head&gt;
  457. &lt;body&gt; &lt;div id=&quot;main&quot;&gt;&lt;/div&gt; &lt;/body&gt;
  458. {glue}
  459. &lt;/html&gt;
  460. &quot;#,
  461. // Create the glue code to connect to the WebSocket on the &quot;/ws&quot; route
  462. glue = dioxus_liveview::interpreter_glue(&amp;format!(&quot;ws://{addr}/ws&quot;))
  463. ))
  464. }),
  465. )
  466. // The WebSocket route is what Dioxus uses to communicate with the browser
  467. .route(
  468. &quot;/ws&quot;,
  469. get(move |ws: WebSocketUpgrade| async move {
  470. ws.on_upgrade(move |socket| async move {
  471. // When the WebSocket is upgraded, launch the LiveView with the app component
  472. _ = view.launch(dioxus_liveview::axum_socket(socket), app).await;
  473. })
  474. }),
  475. );
  476. println!(&quot;Listening on http://{addr}&quot;);
  477. axum::Server::bind(&amp;addr.to_string().parse().unwrap())
  478. .serve(app.into_make_service())
  479. .await
  480. .unwrap();
  481. }
  482. </code></pre></pre>
  483. <p>And then add our app component:</p>
  484. <pre><pre class="playground"><code class="language-rust edition2018">
  485. <span class="boring">#![allow(unused)]
  486. </span><span class="boring">fn main() {
  487. </span>fn app(cx: Scope) -&gt; Element {
  488. cx.render(rsx! {
  489. div {
  490. &quot;Hello, world!&quot;
  491. }
  492. })
  493. }
  494. <span class="boring">}
  495. </span></code></pre></pre>
  496. <p>And that's it!</p>
  497. <div style="break-before: page; page-break-before: always;"></div><h1 id="terminal-ui"><a class="header" href="#terminal-ui">Terminal UI</a></h1>
  498. <p>You can build a text-based interface that will run in the terminal using Dioxus.</p>
  499. <p><img src="https://github.com/DioxusLabs/rink/raw/master/examples/example.png" alt="Hello World screenshot" /></p>
  500. <blockquote>
  501. <p>Note: this book was written with HTML-based platforms in mind. You might be able to follow along with TUI, but you'll have to adapt a bit.</p>
  502. </blockquote>
  503. <h2 id="support-3"><a class="header" href="#support-3">Support</a></h2>
  504. <p>TUI support is currently quite experimental. But, if you're willing to venture into the realm of the unknown, this guide will get you started.</p>
  505. <ul>
  506. <li>It uses flexbox for the layout</li>
  507. <li>It only supports a subset of the attributes and elements</li>
  508. <li>Regular widgets will not work in the tui render, but the tui renderer has its own widget components that start with a capital letter. See the <a href="https://github.com/DioxusLabs/dioxus/blob/master/packages/dioxus-tui/examples/widgets.rs">widgets example</a></li>
  509. <li>1px is one character line height. Your regular CSS px does not translate</li>
  510. <li>If your app panics, your terminal is wrecked. This will be fixed eventually</li>
  511. </ul>
  512. <h2 id="getting-set-up"><a class="header" href="#getting-set-up">Getting Set up</a></h2>
  513. <p>Start by making a new package and adding Dioxus and the TUI renderer as dependancies.</p>
  514. <pre><code class="language-shell">cargo new --bin demo
  515. cd demo
  516. cargo add dioxus
  517. cargo add dioxus-tui
  518. </code></pre>
  519. <p>Then, edit your <code>main.rs</code> with the basic template.</p>
  520. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case)]
  521. // import the prelude to get access to the `rsx!` macro and the `Scope` and `Element` types
  522. use dioxus::prelude::*;
  523. fn main() {
  524. // launch the app in the terminal
  525. dioxus_tui::launch(App);
  526. }
  527. // create a component that renders a div with the text &quot;Hello, world!&quot;
  528. fn App(cx: Scope) -&gt; Element {
  529. cx.render(rsx! {
  530. div {
  531. &quot;Hello, world!&quot;
  532. }
  533. })
  534. }
  535. </code></pre></pre>
  536. <p>To run our app:</p>
  537. <pre><code class="language-shell">cargo run
  538. </code></pre>
  539. <p>Press &quot;ctrl-c&quot; to close the app. To switch from &quot;ctrl-c&quot; to just &quot;q&quot; to quit you can launch the app with a configuration to disable the default quit and use the root TuiContext to quit on your own.</p>
  540. <pre><pre class="playground"><code class="language-rust edition2018">// todo remove deprecated
  541. #![allow(non_snake_case, deprecated)]
  542. use dioxus::events::{KeyCode, KeyboardEvent};
  543. use dioxus::prelude::*;
  544. use dioxus_tui::TuiContext;
  545. fn main() {
  546. dioxus_tui::launch_cfg(
  547. App,
  548. dioxus_tui::Config::new()
  549. .without_ctrl_c_quit()
  550. // Some older terminals only support 16 colors or ANSI colors
  551. // If your terminal is one of these, change this to BaseColors or ANSI
  552. .with_rendering_mode(dioxus_tui::RenderingMode::Rgb),
  553. );
  554. }
  555. fn App(cx: Scope) -&gt; Element {
  556. let tui_ctx: TuiContext = cx.consume_context().unwrap();
  557. cx.render(rsx! {
  558. div {
  559. width: &quot;100%&quot;,
  560. height: &quot;10px&quot;,
  561. background_color: &quot;red&quot;,
  562. justify_content: &quot;center&quot;,
  563. align_items: &quot;center&quot;,
  564. onkeydown: move |k: KeyboardEvent| if let KeyCode::Q = k.key_code {
  565. tui_ctx.quit();
  566. },
  567. &quot;Hello world!&quot;
  568. }
  569. })
  570. }
  571. </code></pre></pre>
  572. <div style="break-before: page; page-break-before: always;"></div><h1 id="mobile-app"><a class="header" href="#mobile-app">Mobile App</a></h1>
  573. <p>Build a mobile app with Dioxus!</p>
  574. <p>Example: <a href="https://github.com/DioxusLabs/example-projects/blob/master/ios_demo">Todo App</a></p>
  575. <h2 id="support-4"><a class="header" href="#support-4">Support</a></h2>
  576. <p>Mobile is currently the least-supported renderer target for Dioxus. Mobile apps are rendered with either the platform's WebView or experimentally through <a href="https://github.com/DioxusLabs/blitz">WGPU</a>. WebView doesn't support animations, transparency, and native widgets.</p>
  577. <p>Mobile support is currently best suited for CRUD-style apps, ideally for internal teams who need to develop quickly but don't care much about animations or native widgets.</p>
  578. <p>This guide is primarily targeted at iOS apps, however, you can follow it while using the <code>android</code> guide in <code>cargo-mobile</code>.</p>
  579. <h2 id="getting-set-up-1"><a class="header" href="#getting-set-up-1">Getting Set up</a></h2>
  580. <p>Getting set up with mobile can be quite challenging. The tooling here isn't great (yet) and might take some hacking around to get things working. macOS M1 is broadly unexplored and might not work for you.</p>
  581. <p>We're going to be using <code>cargo-mobile</code> to build for mobile. First, install it:</p>
  582. <pre><code class="language-shell">cargo install --git https://github.com/BrainiumLLC/cargo-mobile
  583. </code></pre>
  584. <p>And then initialize your app for the right platform. Use the <code>winit</code> template for now. Right now, there's no &quot;Dioxus&quot; template in cargo-mobile.</p>
  585. <pre><code class="language-shell">cargo mobile init
  586. </code></pre>
  587. <p>We're going to completely clear out the <code>dependencies</code> it generates for us, swapping out <code>winit</code> with <code>dioxus-mobile</code>.</p>
  588. <pre><code class="language-toml">
  589. [package]
  590. name = &quot;dioxus-ios-demo&quot;
  591. version = &quot;0.1.0&quot;
  592. authors = []
  593. edition = &quot;2018&quot;
  594. # leave the `lib` declaration
  595. [lib]
  596. crate-type = [&quot;staticlib&quot;, &quot;cdylib&quot;, &quot;rlib&quot;]
  597. # leave the binary it generates for us
  598. [[bin]]
  599. name = &quot;dioxus-ios-demo-desktop&quot;
  600. path = &quot;gen/bin/desktop.rs&quot;
  601. # clear all the dependencies
  602. [dependencies]
  603. mobile-entry-point = &quot;0.1.0&quot;
  604. dioxus = { version = &quot;*&quot;}
  605. dioxus-desktop = { version = &quot;*&quot; }
  606. simple_logger = &quot;*&quot;
  607. </code></pre>
  608. <p>Edit your <code>lib.rs</code>:</p>
  609. <pre><pre class="playground"><code class="language-rust edition2018">use dioxus::prelude::*;
  610. fn main() {
  611. dioxus_desktop::launch(app);
  612. }
  613. fn app(cx: Scope) -&gt; Element {
  614. cx.render(rsx!{
  615. div {
  616. &quot;hello world!&quot;
  617. }
  618. })
  619. }
  620. </code></pre></pre>
  621. <div style="break-before: page; page-break-before: always;"></div><h1 id="setting-up-hot-reload"><a class="header" href="#setting-up-hot-reload">Setting Up Hot Reload</a></h1>
  622. <ol>
  623. <li>Hot reloading allows much faster iteration times inside of rsx calls by interpreting them and streaming the edits.</li>
  624. <li>It is useful when changing the styling/layout of a program, but will not help with changing the logic of a program.</li>
  625. <li>Currently the cli only implements hot reloading for the web renderer. For TUI, desktop, and LiveView you can use the hot reload macro instead.</li>
  626. </ol>
  627. <h1 id="web-1"><a class="header" href="#web-1">Web</a></h1>
  628. <p>For the web renderer, you can use the dioxus cli to serve your application with hot reloading enabled.</p>
  629. <h2 id="setup-2"><a class="header" href="#setup-2">Setup</a></h2>
  630. <p>Install <a href="https://github.com/DioxusLabs/cli">dioxus-cli</a>.
  631. Hot reloading is automatically enabled when using the web renderer on debug builds.</p>
  632. <h2 id="usage"><a class="header" href="#usage">Usage</a></h2>
  633. <ol>
  634. <li>Run:</li>
  635. </ol>
  636. <pre><code class="language-bash">dioxus serve --hot-reload
  637. </code></pre>
  638. <ol start="2">
  639. <li>Change some code within a rsx or render macro</li>
  640. <li>Open your localhost in a browser</li>
  641. <li>Save and watch the style change without recompiling</li>
  642. </ol>
  643. <h1 id="desktopliveviewtuiserver"><a class="header" href="#desktopliveviewtuiserver">Desktop/Liveview/TUI/Server</a></h1>
  644. <p>For desktop, LiveView, and tui, you can place the hot reload macro at the top of your main function to enable hot reloading.
  645. Hot reloading is automatically enabled on debug builds.</p>
  646. <p>For more information about hot reloading on native platforms and configuration options see the <a href="https://crates.io/crates/dioxus-hot-reload">dioxus-hot-reload</a> crate.</p>
  647. <h2 id="setup-3"><a class="header" href="#setup-3">Setup</a></h2>
  648. <p>Add the following to your main function:</p>
  649. <pre><pre class="playground"><code class="language-rust edition2018">fn main() {
  650. hot_reload_init!();
  651. // launch your application
  652. }
  653. </code></pre></pre>
  654. <h2 id="usage-1"><a class="header" href="#usage-1">Usage</a></h2>
  655. <ol>
  656. <li>Run:</li>
  657. </ol>
  658. <pre><code class="language-bash">cargo run
  659. </code></pre>
  660. <ol start="2">
  661. <li>Change some code within a rsx or render macro</li>
  662. <li>Save and watch the style change without recompiling</li>
  663. </ol>
  664. <h1 id="limitations"><a class="header" href="#limitations">Limitations</a></h1>
  665. <ol>
  666. <li>The interpreter can only use expressions that existed on the last full recompile. If you introduce a new variable or expression to the rsx call, it will require a full recompile to capture the expression.</li>
  667. <li>Components, Iterators, and some attributes can contain arbitrary rust code and will trigger a full recompile when changed.</li>
  668. </ol>
  669. <div style="break-before: page; page-break-before: always;"></div><h1 id="describing-the-ui"><a class="header" href="#describing-the-ui">Describing the UI</a></h1>
  670. <p>Dioxus is a <em>declarative</em> framework. This means that instead of telling Dioxus what to do (e.g. to &quot;create an element&quot; or &quot;set the color to red&quot;) we simply <em>declare</em> what we want the UI to look like using RSX.</p>
  671. <p>You have already seen a simple example of RSX syntax in the &quot;hello world&quot; application:</p>
  672. <pre><pre class="playground"><code class="language-rust edition2018">
  673. <span class="boring">#![allow(unused)]
  674. </span><span class="boring">fn main() {
  675. </span>// define a component that renders a div with the text &quot;Hello, world!&quot;
  676. fn App(cx: Scope) -&gt; Element {
  677. cx.render(rsx! {
  678. div {
  679. &quot;Hello, world!&quot;
  680. }
  681. })
  682. }
  683. <span class="boring">}
  684. </span></code></pre></pre>
  685. <p>Here, we use the <code>rsx!</code> macro to <em>declare</em> that we want a <code>div</code> element, containing the text <code>&quot;Hello, world!&quot;</code>. Dioxus takes the RSX and constructs a UI from it.</p>
  686. <h2 id="rsx-features"><a class="header" href="#rsx-features">RSX Features</a></h2>
  687. <p>RSX is very similar to HTML in that it describes elements with attributes and children. Here's an empty <code>div</code> element in RSX, as well as the resulting HTML:</p>
  688. <pre><pre class="playground"><code class="language-rust edition2018">
  689. <span class="boring">#![allow(unused)]
  690. </span><span class="boring">fn main() {
  691. </span>cx.render(rsx!(div {
  692. // attributes / listeners
  693. // children
  694. }))
  695. <span class="boring">}
  696. </span></code></pre></pre>
  697. <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;
  698. </code></pre>
  699. <h3 id="attributes"><a class="header" href="#attributes">Attributes</a></h3>
  700. <p>Attributes (and <a href="describing_ui/../interactivity/index.html">listeners</a>) modify the behavior or appearance of the element they are attached to. They are specified inside the <code>{}</code> brackets, using the <code>name: value</code> syntax. You can provide the value as a literal in the RSX:</p>
  701. <pre><pre class="playground"><code class="language-rust edition2018">
  702. <span class="boring">#![allow(unused)]
  703. </span><span class="boring">fn main() {
  704. </span>cx.render(rsx!(a {
  705. href: &quot;https://www.youtube.com/watch?v=dQw4w9WgXcQ&quot;,
  706. class: &quot;primary_button&quot;,
  707. color: &quot;red&quot;,
  708. }))
  709. <span class="boring">}
  710. </span></code></pre></pre>
  711. <pre><code class="language-html">&lt;a href=&quot;https://www.youtube.com/watch?v=dQw4w9WgXcQ&quot; class=&quot;primary_button&quot; autofocus=&quot;true&quot; style=&quot;color: red&quot;&gt;&lt;/a&gt;
  712. </code></pre>
  713. <blockquote>
  714. <p>Note: All attributes defined in <code>dioxus-html</code> follow the snake_case naming convention. They transform their <code>snake_case</code> names to HTML's <code>camelCase</code> attributes.</p>
  715. </blockquote>
  716. <blockquote>
  717. <p>Note: Styles can be used directly outside of the <code>style:</code> attribute. In the above example, <code>color: &quot;red&quot;</code> is turned into <code>style=&quot;color: red&quot;</code>.</p>
  718. </blockquote>
  719. <h4 id="custom-attributes"><a class="header" href="#custom-attributes">Custom Attributes</a></h4>
  720. <p>Dioxus has a pre-configured set of attributes that you can use. RSX is validated at compile time to make sure you didn't specify an invalid attribute. If you want to override this behavior with a custom attribute name, specify the attribute in quotes:</p>
  721. <pre><pre class="playground"><code class="language-rust edition2018">
  722. <span class="boring">#![allow(unused)]
  723. </span><span class="boring">fn main() {
  724. </span> cx.render(rsx!(b {
  725. &quot;customAttribute&quot;: &quot;value&quot;,
  726. }))
  727. <span class="boring">}
  728. </span></code></pre></pre>
  729. <pre><code class="language-html">&lt;b customAttribute=&quot;value&quot;&gt;
  730. &lt;/b&gt;
  731. </code></pre>
  732. <h3 id="interpolation"><a class="header" href="#interpolation">Interpolation</a></h3>
  733. <p>Similarly to how you can <a href="https://doc.rust-lang.org/rust-by-example/hello/print/fmt.html">format</a> Rust strings, you can also interpolate in RSX text. Use <code>{variable}</code> to Display the value of a variable in a string, or <code>{variable:?}</code> to use the Debug representation:</p>
  734. <pre><pre class="playground"><code class="language-rust edition2018">
  735. <span class="boring">#![allow(unused)]
  736. </span><span class="boring">fn main() {
  737. </span>let coordinates = (42, 0);
  738. let country = &quot;es&quot;;
  739. cx.render(rsx!(div {
  740. class: &quot;country-{country}&quot;,
  741. &quot;position&quot;: &quot;{coordinates:?}&quot;,
  742. // arbitrary expressions are allowed,
  743. // as long as they don't contain `{}`
  744. div {
  745. &quot;{country.to_uppercase()}&quot;
  746. },
  747. div {
  748. &quot;{7*6}&quot;
  749. },
  750. // {} can be escaped with {{}}
  751. div {
  752. &quot;{{}}&quot;
  753. },
  754. }))
  755. <span class="boring">}
  756. </span></code></pre></pre>
  757. <pre><code class="language-html">&lt;div class=&quot;country-es&quot; position=&quot;(42, 0)&quot;&gt;
  758. &lt;div&gt;ES&lt;/div&gt;
  759. &lt;div&gt;42&lt;/div&gt;
  760. &lt;div&gt;{}&lt;/div&gt;
  761. &lt;/div&gt;
  762. </code></pre>
  763. <h3 id="children"><a class="header" href="#children">Children</a></h3>
  764. <p>To add children to an element, put them inside the <code>{}</code> brackets after all attributes and listeners in the element. They can be other elements, text, or <a href="describing_ui/components.html">components</a>. For example, you could have an <code>ol</code> (ordered list) element, containing 3 <code>li</code> (list item) elements, each of which contains some text:</p>
  765. <pre><pre class="playground"><code class="language-rust edition2018">
  766. <span class="boring">#![allow(unused)]
  767. </span><span class="boring">fn main() {
  768. </span>cx.render(rsx!(ol {
  769. li {&quot;First Item&quot;}
  770. li {&quot;Second Item&quot;}
  771. li {&quot;Third Item&quot;}
  772. }))
  773. <span class="boring">}
  774. </span></code></pre></pre>
  775. <pre><code class="language-html">&lt;ol&gt;
  776. &lt;li&gt;First Item&lt;/li&gt;
  777. &lt;li&gt;Second Item&lt;/li&gt;
  778. &lt;li&gt;Third Item&lt;/li&gt;
  779. &lt;/ol&gt;
  780. </code></pre>
  781. <h3 id="fragments"><a class="header" href="#fragments">Fragments</a></h3>
  782. <p>You can render multiple elements at the top level of <code>rsx!</code> and they will be automatically grouped.</p>
  783. <pre><pre class="playground"><code class="language-rust edition2018">
  784. <span class="boring">#![allow(unused)]
  785. </span><span class="boring">fn main() {
  786. </span>cx.render(rsx!(
  787. p {&quot;First Item&quot;},
  788. p {&quot;Second Item&quot;},
  789. ))
  790. <span class="boring">}
  791. </span></code></pre></pre>
  792. <pre><code class="language-html">&lt;p&gt;First Item&lt;/p&gt;
  793. &lt;p&gt;Second Item&lt;/p&gt;
  794. </code></pre>
  795. <h3 id="expressions"><a class="header" href="#expressions">Expressions</a></h3>
  796. <p>You can include arbitrary Rust expressions as children within RSX that implements <a href="https://docs.rs/dioxus-core/0.3/dioxus_core/trait.IntoDynNode.html">IntoDynNode</a>. This is useful for displaying data from an <a href="https://doc.rust-lang.org/stable/book/ch13-02-iterators.html#processing-a-series-of-items-with-iterators">iterator</a>:</p>
  797. <pre><pre class="playground"><code class="language-rust edition2018">
  798. <span class="boring">#![allow(unused)]
  799. </span><span class="boring">fn main() {
  800. </span>let text = &quot;Dioxus&quot;;
  801. cx.render(rsx!(span {
  802. text.to_uppercase(),
  803. // create a list of text from 0 to 9
  804. (0..10).map(|i| rsx!{ i.to_string() })
  805. }))
  806. <span class="boring">}
  807. </span></code></pre></pre>
  808. <pre><code class="language-html">&lt;span&gt;DIOXUS0123456789&lt;/span&gt;
  809. </code></pre>
  810. <h3 id="loops"><a class="header" href="#loops">Loops</a></h3>
  811. <p>In addition to iterators you can also use for loops directly within RSX:</p>
  812. <pre><pre class="playground"><code class="language-rust edition2018">
  813. <span class="boring">#![allow(unused)]
  814. </span><span class="boring">fn main() {
  815. </span>cx.render(rsx!{
  816. // use a for loop where the body itself is RSX
  817. div {
  818. // create a list of text from 0 to 9
  819. for i in 0..3 {
  820. // NOTE: the body of the loop is RSX not a rust statement
  821. div {
  822. &quot;{i}&quot;
  823. }
  824. }
  825. }
  826. // iterator equivalent
  827. div {
  828. (0..3).map(|i| rsx!{ div { &quot;{i}&quot; } })
  829. }
  830. })
  831. <span class="boring">}
  832. </span></code></pre></pre>
  833. <pre><code class="language-html">&lt;div&gt;0&lt;/div&gt;
  834. &lt;div&gt;1&lt;/div&gt;
  835. &lt;div&gt;2&lt;/div&gt;
  836. &lt;div&gt;0&lt;/div&gt;
  837. &lt;div&gt;1&lt;/div&gt;
  838. &lt;div&gt;2&lt;/div&gt;
  839. </code></pre>
  840. <h3 id="if-statements"><a class="header" href="#if-statements">If statements</a></h3>
  841. <p>You can also use if statements without an else branch within RSX:</p>
  842. <pre><pre class="playground"><code class="language-rust edition2018">
  843. <span class="boring">#![allow(unused)]
  844. </span><span class="boring">fn main() {
  845. </span>cx.render(rsx!{
  846. // use if statements without an else
  847. if true {
  848. rsx!(div { &quot;true&quot; })
  849. }
  850. })
  851. <span class="boring">}
  852. </span></code></pre></pre>
  853. <pre><code class="language-html">&lt;div&gt;true&lt;/div&gt;
  854. </code></pre>
  855. <div style="break-before: page; page-break-before: always;"></div><h1 id="special-attributes"><a class="header" href="#special-attributes">Special Attributes</a></h1>
  856. <p>While most attributes are simply passed on to the HTML, some have special behaviors.</p>
  857. <h2 id="the-html-escape-hatch"><a class="header" href="#the-html-escape-hatch">The HTML Escape Hatch</a></h2>
  858. <p>If you're working with pre-rendered assets, output from templates, or output from a JS library, then you might want to pass HTML directly instead of going through Dioxus. In these instances, reach for <code>dangerous_inner_html</code>.</p>
  859. <p>For example, shipping a markdown-to-Dioxus converter might significantly bloat your final application size. Instead, you'll want to pre-render your markdown to HTML and then include the HTML directly in your output. We use this approach for the <a href="https://dioxuslabs.com">Dioxus homepage</a>:</p>
  860. <pre><pre class="playground"><code class="language-rust edition2018">
  861. <span class="boring">#![allow(unused)]
  862. </span><span class="boring">fn main() {
  863. </span>// this should come from a trusted source
  864. let contents = &quot;live &lt;b&gt;dangerously&lt;/b&gt;&quot;;
  865. cx.render(rsx! {
  866. div {
  867. dangerous_inner_html: &quot;{contents}&quot;,
  868. }
  869. })
  870. <span class="boring">}
  871. </span></code></pre></pre>
  872. <blockquote>
  873. <p>Note! This attribute is called &quot;dangerous_inner_html&quot; because it is <strong>dangerous</strong> to pass it data you don't trust. If you're not careful, you can easily expose <a href="https://en.wikipedia.org/wiki/Cross-site_scripting">cross-site scripting (XSS)</a> attacks to your users.</p>
  874. <p>If you're handling untrusted input, make sure to sanitize your HTML before passing it into <code>dangerous_inner_html</code> – or just pass it to a Text Element to escape any HTML tags.</p>
  875. </blockquote>
  876. <h2 id="boolean-attributes"><a class="header" href="#boolean-attributes">Boolean Attributes</a></h2>
  877. <p>Most attributes, when rendered, will be rendered exactly as the input you provided. However, some attributes are considered &quot;boolean&quot; attributes and just their presence determines whether they affect the output. For these attributes, a provided value of <code>&quot;false&quot;</code> will cause them to be removed from the target element.</p>
  878. <p>So this RSX wouldn't actually render the <code>hidden</code> attribute:</p>
  879. <pre><pre class="playground"><code class="language-rust edition2018">
  880. <span class="boring">#![allow(unused)]
  881. </span><span class="boring">fn main() {
  882. </span>cx.render(rsx! {
  883. div {
  884. hidden: &quot;false&quot;,
  885. &quot;hello&quot;
  886. }
  887. })
  888. <span class="boring">}
  889. </span></code></pre></pre>
  890. <pre><code class="language-html">&lt;div&gt;hello&lt;/div&gt;
  891. </code></pre>
  892. <p>Not all attributes work like this however. <em>Only the following attributes</em> have this behavior:</p>
  893. <ul>
  894. <li><code>allowfullscreen</code></li>
  895. <li><code>allowpaymentrequest</code></li>
  896. <li><code>async</code></li>
  897. <li><code>autofocus</code></li>
  898. <li><code>autoplay</code></li>
  899. <li><code>checked</code></li>
  900. <li><code>controls</code></li>
  901. <li><code>default</code></li>
  902. <li><code>defer</code></li>
  903. <li><code>disabled</code></li>
  904. <li><code>formnovalidate</code></li>
  905. <li><code>hidden</code></li>
  906. <li><code>ismap</code></li>
  907. <li><code>itemscope</code></li>
  908. <li><code>loop</code></li>
  909. <li><code>multiple</code></li>
  910. <li><code>muted</code></li>
  911. <li><code>nomodule</code></li>
  912. <li><code>novalidate</code></li>
  913. <li><code>open</code></li>
  914. <li><code>playsinline</code></li>
  915. <li><code>readonly</code></li>
  916. <li><code>required</code></li>
  917. <li><code>reversed</code></li>
  918. <li><code>selected</code></li>
  919. <li><code>truespeed</code></li>
  920. </ul>
  921. <p>For any other attributes, a value of <code>&quot;false&quot;</code> will be sent directly to the DOM.</p>
  922. <div style="break-before: page; page-break-before: always;"></div><h1 id="components"><a class="header" href="#components">Components</a></h1>
  923. <p>Just like you wouldn't want to write a complex program in a single, long, <code>main</code> function, you shouldn't build a complex UI in a single <code>App</code> function. Instead, you should break down the functionality of an app in logical parts called components.</p>
  924. <p>A component is a Rust function, named in UpperCammelCase, that takes a <code>Scope</code> parameter and returns an <code>Element</code> describing the UI it wants to render. In fact, our <code>App</code> function is a component!</p>
  925. <pre><pre class="playground"><code class="language-rust edition2018">
  926. <span class="boring">#![allow(unused)]
  927. </span><span class="boring">fn main() {
  928. </span>// define a component that renders a div with the text &quot;Hello, world!&quot;
  929. fn App(cx: Scope) -&gt; Element {
  930. cx.render(rsx! {
  931. div {
  932. &quot;Hello, world!&quot;
  933. }
  934. })
  935. }
  936. <span class="boring">}
  937. </span></code></pre></pre>
  938. <blockquote>
  939. <p>You'll probably want to add <code>#![allow(non_snake_case)]</code> to the top of your crate to avoid warnings about UpperCammelCase component names</p>
  940. </blockquote>
  941. <p>A Component is responsible for some rendering task – typically, rendering an isolated part of the user interface. For example, you could have an <code>About</code> component that renders a short description of Dioxus Labs:</p>
  942. <pre><pre class="playground"><code class="language-rust edition2018">
  943. <span class="boring">#![allow(unused)]
  944. </span><span class="boring">fn main() {
  945. </span>pub fn About(cx: Scope) -&gt; Element {
  946. cx.render(rsx!(p {
  947. b {&quot;Dioxus Labs&quot;}
  948. &quot; An Open Source project dedicated to making Rust UI wonderful.&quot;
  949. }))
  950. }
  951. <span class="boring">}
  952. </span></code></pre></pre>
  953. <p>Then, you can render your component in another component, similarly to how elements are rendered:</p>
  954. <pre><pre class="playground"><code class="language-rust edition2018">
  955. <span class="boring">#![allow(unused)]
  956. </span><span class="boring">fn main() {
  957. </span>fn App(cx: Scope) -&gt; Element {
  958. cx.render(rsx! {
  959. About {},
  960. About {},
  961. })
  962. }
  963. <span class="boring">}
  964. </span></code></pre></pre>
  965. <p><img src="describing_ui/./images/screenshot_about_component.png" alt="Screenshot containing the About component twice" /></p>
  966. <blockquote>
  967. <p>At this point, it might seem like components are nothing more than functions. However, as you learn more about the features of Dioxus, you'll see that they are actually more powerful!</p>
  968. </blockquote>
  969. <div style="break-before: page; page-break-before: always;"></div><h1 id="component-props"><a class="header" href="#component-props">Component Props</a></h1>
  970. <p>Just like you can pass arguments to a function, you can pass props to a component that customize its behavior! The components we've seen so far didn't accept any props – so let's write some components that do.</p>
  971. <h2 id="deriveprops"><a class="header" href="#deriveprops"><code>#[derive(Props)]</code></a></h2>
  972. <p>Component props are a single struct annotated with <code>#[derive(Props)]</code>. For a component to accept props, the type of its argument must be <code>Scope&lt;YourPropsStruct&gt;</code>. Then, you can access the value of the props using <code>cx.props</code>.</p>
  973. <p>There are 2 flavors of Props structs:</p>
  974. <ul>
  975. <li>Owned props:
  976. <ul>
  977. <li>Don't have an associated lifetime</li>
  978. <li>Implement <code>PartialEq</code>, allow for memoization (if the props don't change, Dioxus won't re-render the component)</li>
  979. </ul>
  980. </li>
  981. <li>Borrowed props:
  982. <ul>
  983. <li><a href="https://doc.rust-lang.org/beta/rust-by-example/scope/borrow.html">Borrow</a> from a parent component</li>
  984. <li>Cannot be memoized due to lifetime constraints</li>
  985. </ul>
  986. </li>
  987. </ul>
  988. <h3 id="owned-props"><a class="header" href="#owned-props">Owned Props</a></h3>
  989. <p>Owned Props are very simple – they don't borrow anything. Example:</p>
  990. <pre><pre class="playground"><code class="language-rust edition2018">
  991. <span class="boring">#![allow(unused)]
  992. </span><span class="boring">fn main() {
  993. </span>// Remember: Owned props must implement `PartialEq`!
  994. #[derive(PartialEq, Props)]
  995. struct LikesProps {
  996. score: i32,
  997. }
  998. fn Likes(cx: Scope&lt;LikesProps&gt;) -&gt; Element {
  999. cx.render(rsx! {
  1000. div {
  1001. &quot;This post has &quot;,
  1002. b { &quot;{cx.props.score}&quot; },
  1003. &quot; likes&quot;
  1004. }
  1005. })
  1006. }
  1007. <span class="boring">}
  1008. </span></code></pre></pre>
  1009. <p>You can then pass prop values to the component the same way you would pass attributes to an element:</p>
  1010. <pre><pre class="playground"><code class="language-rust edition2018">
  1011. <span class="boring">#![allow(unused)]
  1012. </span><span class="boring">fn main() {
  1013. </span>fn App(cx: Scope) -&gt; Element {
  1014. cx.render(rsx! {
  1015. Likes {
  1016. score: 42,
  1017. },
  1018. })
  1019. }
  1020. <span class="boring">}
  1021. </span></code></pre></pre>
  1022. <p><img src="describing_ui/./images/component_owned_props_screenshot.png" alt="Screenshot: Likes component" /></p>
  1023. <h3 id="borrowed-props"><a class="header" href="#borrowed-props">Borrowed Props</a></h3>
  1024. <p>Owned props work well if your props are easy to copy around – like a single number. But what if we need to pass a larger data type, like a String from an <code>App</code> Component to a <code>TitleCard</code> subcomponent? A naive solution might be to <a href="https://doc.rust-lang.org/std/clone/trait.Clone.html"><code>.clone()</code></a> the String, creating a copy of it for the subcomponent – but this would be inefficient, especially for larger Strings.</p>
  1025. <p>Rust allows for something more efficient – borrowing the String as a <code>&amp;str</code> – this is what Borrowed Props are for!</p>
  1026. <pre><pre class="playground"><code class="language-rust edition2018">
  1027. <span class="boring">#![allow(unused)]
  1028. </span><span class="boring">fn main() {
  1029. </span>#[derive(Props)]
  1030. struct TitleCardProps&lt;'a&gt; {
  1031. title: &amp;'a str,
  1032. }
  1033. fn TitleCard&lt;'a&gt;(cx: Scope&lt;'a, TitleCardProps&lt;'a&gt;&gt;) -&gt; Element {
  1034. cx.render(rsx! {
  1035. h1 { &quot;{cx.props.title}&quot; }
  1036. })
  1037. }
  1038. <span class="boring">}
  1039. </span></code></pre></pre>
  1040. <p>We can then use the component like this:</p>
  1041. <pre><pre class="playground"><code class="language-rust edition2018">
  1042. <span class="boring">#![allow(unused)]
  1043. </span><span class="boring">fn main() {
  1044. </span>fn App(cx: Scope) -&gt; Element {
  1045. let hello = &quot;Hello Dioxus!&quot;;
  1046. cx.render(rsx!(TitleCard { title: hello }))
  1047. }
  1048. <span class="boring">}
  1049. </span></code></pre></pre>
  1050. <p><img src="describing_ui/./images/component_borrowed_props_screenshot.png" alt="Screenshot: TitleCard component" /></p>
  1051. <p>Borrowed props can be very useful, but they do not allow for memorization so they will <em>always</em> rerun when the parent scope is rerendered. Because of this Borrowed Props should be reserved for components that are cheap to rerun or places where cloning data is an issue. Using Borrowed Props everywhere will result in large parts of your app rerunning every interaction.</p>
  1052. <h2 id="prop-options"><a class="header" href="#prop-options">Prop Options</a></h2>
  1053. <p>The <code>#[derive(Props)]</code> macro has some features that let you customize the behavior of props.</p>
  1054. <h3 id="optional-props"><a class="header" href="#optional-props">Optional Props</a></h3>
  1055. <p>You can create optional fields by using the <code>Option&lt;…&gt;</code> type for a field:</p>
  1056. <pre><pre class="playground"><code class="language-rust edition2018">
  1057. <span class="boring">#![allow(unused)]
  1058. </span><span class="boring">fn main() {
  1059. </span>#[derive(Props)]
  1060. struct OptionalProps&lt;'a&gt; {
  1061. title: &amp;'a str,
  1062. subtitle: Option&lt;&amp;'a str&gt;,
  1063. }
  1064. fn Title&lt;'a&gt;(cx: Scope&lt;'a, OptionalProps&gt;) -&gt; Element&lt;'a&gt; {
  1065. cx.render(rsx!(h1{
  1066. &quot;{cx.props.title}: &quot;,
  1067. cx.props.subtitle.unwrap_or(&quot;No subtitle provided&quot;),
  1068. }))
  1069. }
  1070. <span class="boring">}
  1071. </span></code></pre></pre>
  1072. <p>Then, you can choose to either provide them or not:</p>
  1073. <pre><pre class="playground"><code class="language-rust edition2018">
  1074. <span class="boring">#![allow(unused)]
  1075. </span><span class="boring">fn main() {
  1076. </span>Title {
  1077. title: &quot;Some Title&quot;,
  1078. },
  1079. Title {
  1080. title: &quot;Some Title&quot;,
  1081. subtitle: &quot;Some Subtitle&quot;,
  1082. },
  1083. // Providing an Option explicitly won't compile though:
  1084. // Title {
  1085. // title: &quot;Some Title&quot;,
  1086. // subtitle: None,
  1087. // },
  1088. <span class="boring">}
  1089. </span></code></pre></pre>
  1090. <h3 id="explicitly-required-options"><a class="header" href="#explicitly-required-options">Explicitly Required <code>Option</code>s</a></h3>
  1091. <p>If you want to explicitly require an <code>Option</code>, and not an optional prop, you can annotate it with <code>#[props(!optional)]</code>:</p>
  1092. <pre><pre class="playground"><code class="language-rust edition2018">
  1093. <span class="boring">#![allow(unused)]
  1094. </span><span class="boring">fn main() {
  1095. </span>#[derive(Props)]
  1096. struct ExplicitOptionProps&lt;'a&gt; {
  1097. title: &amp;'a str,
  1098. #[props(!optional)]
  1099. subtitle: Option&lt;&amp;'a str&gt;,
  1100. }
  1101. fn ExplicitOption&lt;'a&gt;(cx: Scope&lt;'a, ExplicitOptionProps&gt;) -&gt; Element&lt;'a&gt; {
  1102. cx.render(rsx!(h1 {
  1103. &quot;{cx.props.title}: &quot;,
  1104. cx.props.subtitle.unwrap_or(&quot;No subtitle provided&quot;),
  1105. }))
  1106. }
  1107. <span class="boring">}
  1108. </span></code></pre></pre>
  1109. <p>Then, you have to explicitly pass either <code>Some(&quot;str&quot;)</code> or <code>None</code>:</p>
  1110. <pre><pre class="playground"><code class="language-rust edition2018">
  1111. <span class="boring">#![allow(unused)]
  1112. </span><span class="boring">fn main() {
  1113. </span>ExplicitOption {
  1114. title: &quot;Some Title&quot;,
  1115. subtitle: None,
  1116. },
  1117. ExplicitOption {
  1118. title: &quot;Some Title&quot;,
  1119. subtitle: Some(&quot;Some Title&quot;),
  1120. },
  1121. // This won't compile:
  1122. // ExplicitOption {
  1123. // title: &quot;Some Title&quot;,
  1124. // },
  1125. <span class="boring">}
  1126. </span></code></pre></pre>
  1127. <h3 id="default-props"><a class="header" href="#default-props">Default Props</a></h3>
  1128. <p>You can use <code>#[props(default = 42)]</code> to make a field optional and specify its default value:</p>
  1129. <pre><pre class="playground"><code class="language-rust edition2018">
  1130. <span class="boring">#![allow(unused)]
  1131. </span><span class="boring">fn main() {
  1132. </span>#[derive(PartialEq, Props)]
  1133. struct DefaultProps {
  1134. // default to 42 when not provided
  1135. #[props(default = 42)]
  1136. number: i64,
  1137. }
  1138. fn DefaultComponent(cx: Scope&lt;DefaultProps&gt;) -&gt; Element {
  1139. cx.render(rsx!(h1 { &quot;{cx.props.number}&quot; }))
  1140. }
  1141. <span class="boring">}
  1142. </span></code></pre></pre>
  1143. <p>Then, similarly to optional props, you don't have to provide it:</p>
  1144. <pre><pre class="playground"><code class="language-rust edition2018">
  1145. <span class="boring">#![allow(unused)]
  1146. </span><span class="boring">fn main() {
  1147. </span>DefaultComponent {
  1148. number: 5,
  1149. },
  1150. DefaultComponent {},
  1151. <span class="boring">}
  1152. </span></code></pre></pre>
  1153. <h3 id="automatic-conversion-with-into"><a class="header" href="#automatic-conversion-with-into">Automatic Conversion with <code>.into</code></a></h3>
  1154. <p>It is common for Rust functions to accept <code>impl Into&lt;SomeType&gt;</code> rather than just <code>SomeType</code> to support a wider range of parameters. If you want similar functionality with props, you can use <code>#[props(into)]</code>. For example, you could add it on a <code>String</code> prop – and <code>&amp;str</code> will also be automatically accepted, as it can be converted into <code>String</code>:</p>
  1155. <pre><pre class="playground"><code class="language-rust edition2018">
  1156. <span class="boring">#![allow(unused)]
  1157. </span><span class="boring">fn main() {
  1158. </span>#[derive(PartialEq, Props)]
  1159. struct IntoProps {
  1160. #[props(into)]
  1161. string: String,
  1162. }
  1163. fn IntoComponent(cx: Scope&lt;IntoProps&gt;) -&gt; Element {
  1164. cx.render(rsx!(h1 { &quot;{cx.props.string}&quot; }))
  1165. }
  1166. <span class="boring">}
  1167. </span></code></pre></pre>
  1168. <p>Then, you can use it so:</p>
  1169. <pre><pre class="playground"><code class="language-rust edition2018">
  1170. <span class="boring">#![allow(unused)]
  1171. </span><span class="boring">fn main() {
  1172. </span>IntoComponent {
  1173. string: &quot;some &amp;str&quot;,
  1174. },
  1175. <span class="boring">}
  1176. </span></code></pre></pre>
  1177. <h2 id="the-inline_props-macro"><a class="header" href="#the-inline_props-macro">The <code>inline_props</code> macro</a></h2>
  1178. <p>So far, every Component function we've seen had a corresponding ComponentProps struct to pass in props. This was quite verbose... Wouldn't it be nice to have props as simple function arguments? Then we wouldn't need to define a Props struct, and instead of typing <code>cx.props.whatever</code>, we could just use <code>whatever</code> directly!</p>
  1179. <p><code>inline_props</code> allows you to do just that. Instead of typing the &quot;full&quot; version:</p>
  1180. <pre><pre class="playground"><code class="language-rust edition2018">
  1181. <span class="boring">#![allow(unused)]
  1182. </span><span class="boring">fn main() {
  1183. </span>#[derive(Props, PartialEq)]
  1184. struct TitleCardProps {
  1185. title: String,
  1186. }
  1187. fn TitleCard(cx: Scope&lt;TitleCardProps&gt;) -&gt; Element {
  1188. cx.render(rsx!{
  1189. h1 { &quot;{cx.props.title}&quot; }
  1190. })
  1191. }
  1192. <span class="boring">}
  1193. </span></code></pre></pre>
  1194. <p>...you can define a function that accepts props as arguments. Then, just annotate it with <code>#[inline_props]</code>, and the macro will turn it into a regular Component for you:</p>
  1195. <pre><pre class="playground"><code class="language-rust edition2018">
  1196. <span class="boring">#![allow(unused)]
  1197. </span><span class="boring">fn main() {
  1198. </span>#[inline_props]
  1199. fn TitleCard(cx: Scope, title: String) -&gt; Element {
  1200. cx.render(rsx!{
  1201. h1 { &quot;{title}&quot; }
  1202. })
  1203. }
  1204. <span class="boring">}
  1205. </span></code></pre></pre>
  1206. <blockquote>
  1207. <p>While the new Component is shorter and easier to read, this macro should not be used by library authors since you have less control over Prop documentation.</p>
  1208. </blockquote>
  1209. <div style="break-before: page; page-break-before: always;"></div><h1 id="component-children"><a class="header" href="#component-children">Component Children</a></h1>
  1210. <p>In some cases, you may wish to create a component that acts as a container for some other content, without the component needing to know what that content is. To achieve this, create a prop of type <code>Element</code>:</p>
  1211. <pre><pre class="playground"><code class="language-rust edition2018">
  1212. <span class="boring">#![allow(unused)]
  1213. </span><span class="boring">fn main() {
  1214. </span>#[derive(Props)]
  1215. struct ClickableProps&lt;'a&gt; {
  1216. href: &amp;'a str,
  1217. body: Element&lt;'a&gt;,
  1218. }
  1219. fn Clickable&lt;'a&gt;(cx: Scope&lt;'a, ClickableProps&lt;'a&gt;&gt;) -&gt; Element {
  1220. cx.render(rsx!(
  1221. a {
  1222. href: &quot;{cx.props.href}&quot;,
  1223. class: &quot;fancy-button&quot;,
  1224. &amp;cx.props.body
  1225. }
  1226. ))
  1227. }
  1228. <span class="boring">}
  1229. </span></code></pre></pre>
  1230. <p>Then, when rendering the component, you can pass in the output of <code>cx.render(rsx!(...))</code>:</p>
  1231. <pre><pre class="playground"><code class="language-rust edition2018">
  1232. <span class="boring">#![allow(unused)]
  1233. </span><span class="boring">fn main() {
  1234. </span> cx.render(rsx! {
  1235. Clickable {
  1236. href: &quot;https://www.youtube.com/watch?v=C-M2hs3sXGo&quot;,
  1237. body: cx.render(rsx!(&quot;How to &quot; i {&quot;not&quot;} &quot; be seen&quot;)),
  1238. }
  1239. })
  1240. <span class="boring">}
  1241. </span></code></pre></pre>
  1242. <blockquote>
  1243. <p>Note: Since <code>Element&lt;'a&gt;</code> is a borrowed prop, there will be no memoization.</p>
  1244. </blockquote>
  1245. <blockquote>
  1246. <p>Warning: While it may compile, do not include the same <code>Element</code> more than once in the RSX. The resulting behavior is unspecified.</p>
  1247. </blockquote>
  1248. <h2 id="the-children-field"><a class="header" href="#the-children-field">The <code>children</code> field</a></h2>
  1249. <p>Rather than passing the RSX through a regular prop, you may wish to accept children similarly to how elements can have children. The &quot;magic&quot; <code>children</code> prop lets you achieve this:</p>
  1250. <pre><pre class="playground"><code class="language-rust edition2018">
  1251. <span class="boring">#![allow(unused)]
  1252. </span><span class="boring">fn main() {
  1253. </span>#[derive(Props)]
  1254. struct ClickableProps&lt;'a&gt; {
  1255. href: &amp;'a str,
  1256. children: Element&lt;'a&gt;,
  1257. }
  1258. fn Clickable&lt;'a&gt;(cx: Scope&lt;'a, ClickableProps&lt;'a&gt;&gt;) -&gt; Element {
  1259. cx.render(rsx!(
  1260. a {
  1261. href: &quot;{cx.props.href}&quot;,
  1262. class: &quot;fancy-button&quot;,
  1263. &amp;cx.props.children
  1264. }
  1265. ))
  1266. }
  1267. <span class="boring">}
  1268. </span></code></pre></pre>
  1269. <p>This makes using the component much simpler: simply put the RSX inside the <code>{}</code> brackets – and there is no need for a <code>render</code> call or another macro!</p>
  1270. <pre><pre class="playground"><code class="language-rust edition2018">
  1271. <span class="boring">#![allow(unused)]
  1272. </span><span class="boring">fn main() {
  1273. </span> cx.render(rsx! {
  1274. Clickable {
  1275. href: &quot;https://www.youtube.com/watch?v=C-M2hs3sXGo&quot;,
  1276. &quot;How to &quot; i {&quot;not&quot;} &quot; be seen&quot;
  1277. }
  1278. })
  1279. <span class="boring">}
  1280. </span></code></pre></pre>
  1281. <div style="break-before: page; page-break-before: always;"></div><h1 id="interactivity"><a class="header" href="#interactivity">Interactivity</a></h1>
  1282. <p>So far, we've learned how to describe the structure and properties of our user interfaces. However, most interfaces need to be interactive in order to be useful. In this chapter, we describe how to make a Dioxus app that responds to the user.</p>
  1283. <div style="break-before: page; page-break-before: always;"></div><h1 id="event-handlers"><a class="header" href="#event-handlers">Event Handlers</a></h1>
  1284. <p>Event handlers are used to respond to user actions. For example, an event handler could be triggered when the user clicks, scrolls, moves the mouse, or types a character.</p>
  1285. <p>Event handlers are attached to elements. For example, we usually don't care about all the clicks that happen within an app, only those on a particular button.</p>
  1286. <p>Event handlers are similar to regular attributes, but their name usually starts with <code>on</code>- and they accept closures as values. The closure will be called whenever the event it listens for is triggered and will be passed that event.</p>
  1287. <p>For example, to handle clicks on an element, we can specify an <code>onclick</code> handler:</p>
  1288. <pre><pre class="playground"><code class="language-rust edition2018">
  1289. <span class="boring">#![allow(unused)]
  1290. </span><span class="boring">fn main() {
  1291. </span>cx.render(rsx! {
  1292. button {
  1293. onclick: move |event| println!(&quot;Clicked! Event: {event:?}&quot;),
  1294. &quot;click me!&quot;
  1295. }
  1296. })
  1297. <span class="boring">}
  1298. </span></code></pre></pre>
  1299. <h2 id="the-event-object"><a class="header" href="#the-event-object">The <code>Event</code> object</a></h2>
  1300. <p>Event handlers receive an <a href="https://docs.rs/dioxus-core/latest/dioxus_core/struct.Event.html"><code>Event</code></a> object containing information about the event. Different types of events contain different types of data. For example, mouse-related events contain <a href="https://docs.rs/dioxus/latest/dioxus/events/struct.MouseData.html"><code>MouseData</code></a>, which tells you things like where the mouse was clicked and what mouse buttons were used.</p>
  1301. <p>In the example above, this event data was logged to the terminal:</p>
  1302. <pre><code>Clicked! Event: UiEvent { bubble_state: Cell { value: true }, data: MouseData { coordinates: Coordinates { screen: (242.0, 256.0), client: (26.0, 17.0), element: (16.0, 7.0), page: (26.0, 17.0) }, modifiers: (empty), held_buttons: EnumSet(), trigger_button: Some(Primary) } }
  1303. Clicked! Event: UiEvent { bubble_state: Cell { value: true }, data: MouseData { coordinates: Coordinates { screen: (242.0, 256.0), client: (26.0, 17.0), element: (16.0, 7.0), page: (26.0, 17.0) }, modifiers: (empty), held_buttons: EnumSet(), trigger_button: Some(Primary) } }
  1304. </code></pre>
  1305. <p>To learn what the different event types for HTML provide, read the <a href="https://docs.rs/dioxus-html/latest/dioxus_html/events/index.html">events module docs</a>.</p>
  1306. <h3 id="event-propagation"><a class="header" href="#event-propagation">Event propagation</a></h3>
  1307. <p>Some events will trigger first on the element the event originated at upward. For example, a click event on a <code>button</code> inside a <code>div</code> would first trigger the button's event listener and then the div's event listener.</p>
  1308. <blockquote>
  1309. <p>For more information about event propigation see <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_bubbling">the mdn docs on event bubling</a></p>
  1310. </blockquote>
  1311. <p>If you want to prevent this behavior, you can call <code>stop_propagation()</code> on the event:</p>
  1312. <pre><pre class="playground"><code class="language-rust edition2018">
  1313. <span class="boring">#![allow(unused)]
  1314. </span><span class="boring">fn main() {
  1315. </span>cx.render(rsx! {
  1316. div {
  1317. onclick: move |_event| {},
  1318. &quot;outer&quot;,
  1319. button {
  1320. onclick: move |event| {
  1321. // now, outer won't be triggered
  1322. event.stop_propagation();
  1323. },
  1324. &quot;inner&quot;
  1325. }
  1326. }
  1327. })
  1328. <span class="boring">}
  1329. </span></code></pre></pre>
  1330. <h2 id="prevent-default"><a class="header" href="#prevent-default">Prevent Default</a></h2>
  1331. <p>Some events have a default behavior. For keyboard events, this might be entering the typed character. For mouse events, this might be selecting some text.</p>
  1332. <p>In some instances, might want to avoid this default behavior. For this, you can add the <code>prevent_default</code> attribute with the name of the handler whose default behavior you want to stop. This attribute can be used for multiple handlers using their name separated by spaces:</p>
  1333. <pre><pre class="playground"><code class="language-rust edition2018">
  1334. <span class="boring">#![allow(unused)]
  1335. </span><span class="boring">fn main() {
  1336. </span>cx.render(rsx! {
  1337. input {
  1338. prevent_default: &quot;oninput onclick&quot;,
  1339. }
  1340. })
  1341. <span class="boring">}
  1342. </span></code></pre></pre>
  1343. <p>Any event handlers will still be called.</p>
  1344. <blockquote>
  1345. <p>Normally, in React or JavaScript, you'd call &quot;preventDefault&quot; on the event in the callback. Dioxus does <em>not</em> currently support this behavior. Note: this means you cannot conditionally prevent default behavior based on the data in the event.</p>
  1346. </blockquote>
  1347. <h2 id="handler-props"><a class="header" href="#handler-props">Handler Props</a></h2>
  1348. <p>Sometimes, you might want to make a component that accepts an event handler. A simple example would be a <code>FancyButton</code> component, which accepts an <code>on_click</code> handler:</p>
  1349. <pre><pre class="playground"><code class="language-rust edition2018">
  1350. <span class="boring">#![allow(unused)]
  1351. </span><span class="boring">fn main() {
  1352. </span>#[derive(Props)]
  1353. pub struct FancyButtonProps&lt;'a&gt; {
  1354. on_click: EventHandler&lt;'a, MouseEvent&gt;,
  1355. }
  1356. pub fn FancyButton&lt;'a&gt;(cx: Scope&lt;'a, FancyButtonProps&lt;'a&gt;&gt;) -&gt; Element&lt;'a&gt; {
  1357. cx.render(rsx!(button {
  1358. class: &quot;fancy-button&quot;,
  1359. onclick: move |evt| cx.props.on_click.call(evt),
  1360. &quot;click me pls.&quot;
  1361. }))
  1362. }
  1363. <span class="boring">}
  1364. </span></code></pre></pre>
  1365. <p>Then, you can use it like any other handler:</p>
  1366. <pre><pre class="playground"><code class="language-rust edition2018">
  1367. <span class="boring">#![allow(unused)]
  1368. </span><span class="boring">fn main() {
  1369. </span> cx.render(rsx! {
  1370. FancyButton {
  1371. on_click: move |event| println!(&quot;Clicked! {event:?}&quot;)
  1372. }
  1373. })
  1374. <span class="boring">}
  1375. </span></code></pre></pre>
  1376. <blockquote>
  1377. <p>Note: just like any other attribute, you can name the handlers anything you want! Though they must start with <code>on</code>, for the prop to be automatically turned into an <code>EventHandler</code> at the call site.</p>
  1378. <p>You can also put custom data in the event, rather than e.g. <code>MouseData</code></p>
  1379. </blockquote>
  1380. <div style="break-before: page; page-break-before: always;"></div><h1 id="hooks-and-component-state"><a class="header" href="#hooks-and-component-state">Hooks and Component State</a></h1>
  1381. <p>So far our components have had no state like a normal rust functions. However, in a UI component, it is often useful to have stateful functionality to build user interactions. For example, you might want to track whether the user has opened a drop-down, and render different things accordingly.</p>
  1382. <p>Hooks allow us to create state in our components. Hooks are Rust functions that take a reference to <code>ScopeState</code> (in a component, you can pass <code>cx</code>), and provide you with functionality and state.</p>
  1383. <h2 id="use_state-hook"><a class="header" href="#use_state-hook"><code>use_state</code> Hook</a></h2>
  1384. <p><a href="https://docs.rs/dioxus/latest/dioxus/prelude/fn.use_state.html"><code>use_state</code></a> is one of the simplest hooks.</p>
  1385. <ul>
  1386. <li>You provide a closure that determines the initial value</li>
  1387. <li><code>use_state</code> gives you the current value, and a way to update it by setting it to something else</li>
  1388. <li>When the value updates, <code>use_state</code> makes the component re-render, and provides you with the new value</li>
  1389. </ul>
  1390. <p>For example, you might have seen the counter example, in which state (a number) is tracked using the <code>use_state</code> hook:</p>
  1391. <pre><pre class="playground"><code class="language-rust edition2018">
  1392. <span class="boring">#![allow(unused)]
  1393. </span><span class="boring">fn main() {
  1394. </span>fn App(cx: Scope) -&gt; Element {
  1395. // count will be initialized to 0 the first time the component is rendered
  1396. let mut count = use_state(cx, || 0);
  1397. cx.render(rsx!(
  1398. h1 { &quot;High-Five counter: {count}&quot; }
  1399. button {
  1400. onclick: move |_| {
  1401. // changing the count will cause the component to re-render
  1402. count += 1
  1403. },
  1404. &quot;Up high!&quot;
  1405. }
  1406. button {
  1407. onclick: move |_| {
  1408. // changing the count will cause the component to re-render
  1409. count -= 1
  1410. },
  1411. &quot;Down low!&quot;
  1412. }
  1413. ))
  1414. }
  1415. <span class="boring">}
  1416. </span></code></pre></pre>
  1417. <p><img src="interactivity/./images/counter.png" alt="Screenshot: counter app" /></p>
  1418. <p>Every time the component's state changes, it re-renders, and the component function is called, so you can describe what you want the new UI to look like. You don't have to worry about &quot;changing&quot; anything – just describe what you want in terms of the state, and Dioxus will take care of the rest!</p>
  1419. <blockquote>
  1420. <p><code>use_state</code> returns your value wrapped in a smart pointer of type <a href="https://docs.rs/dioxus/latest/dioxus/prelude/struct.UseState.html"><code>UseState</code></a>. This is why you can both read the value and update it, even within an event handler.</p>
  1421. </blockquote>
  1422. <p>You can use multiple hooks in the same component if you want:</p>
  1423. <pre><pre class="playground"><code class="language-rust edition2018">
  1424. <span class="boring">#![allow(unused)]
  1425. </span><span class="boring">fn main() {
  1426. </span>fn App(cx: Scope) -&gt; Element {
  1427. let mut count_a = use_state(cx, || 0);
  1428. let mut count_b = use_state(cx, || 0);
  1429. cx.render(rsx!(
  1430. h1 { &quot;Counter_a: {count_a}&quot; }
  1431. button { onclick: move |_| count_a += 1, &quot;a++&quot; }
  1432. button { onclick: move |_| count_a -= 1, &quot;a--&quot; }
  1433. h1 { &quot;Counter_b: {count_b}&quot; }
  1434. button { onclick: move |_| count_b += 1, &quot;b++&quot; }
  1435. button { onclick: move |_| count_b -= 1, &quot;b--&quot; }
  1436. ))
  1437. }
  1438. <span class="boring">}
  1439. </span></code></pre></pre>
  1440. <p><img src="interactivity/./images/counter_two_state.png" alt="Screenshot: app with two counters" /></p>
  1441. <h2 id="rules-of-hooks"><a class="header" href="#rules-of-hooks">Rules of Hooks</a></h2>
  1442. <p>The above example might seem a bit magic, since Rust functions are typically not associated with state. Dioxus allows hooks to maintain state across renders through a reference to <code>ScopeState</code>, which is why you must pass <code>&amp;cx</code> to them.</p>
  1443. <p>But how can Dioxus differentiate between multiple hooks in the same component? As you saw in the second example, both <code>use_state</code> functions were called with the same parameters, so how come they can return different things when the counters are different?</p>
  1444. <pre><pre class="playground"><code class="language-rust edition2018">
  1445. <span class="boring">#![allow(unused)]
  1446. </span><span class="boring">fn main() {
  1447. </span> let mut count_a = use_state(cx, || 0);
  1448. let mut count_b = use_state(cx, || 0);
  1449. <span class="boring">}
  1450. </span></code></pre></pre>
  1451. <p>This is only possible because the two hooks are always called in the same order, so Dioxus knows which is which. Because the order you call hooks matters, you must follow certain rules when using hooks:</p>
  1452. <ol>
  1453. <li>Hooks may be only used in components or other hooks (we'll get to that later)</li>
  1454. <li>On every call to the component function
  1455. <ol>
  1456. <li>The same hooks must be called (except in the case of early returns, as explained later in the <a href="interactivity/../best_practices/error_handling.html">Error Handling chapter</a>)</li>
  1457. <li>In the same order</li>
  1458. </ol>
  1459. </li>
  1460. <li>Hooks name's should start with <code>use_</code> so you don't accidentally confuse them with regular functions</li>
  1461. </ol>
  1462. <p>These rules mean that there are certain things you can't do with hooks:</p>
  1463. <h3 id="no-hooks-in-conditionals"><a class="header" href="#no-hooks-in-conditionals">No Hooks in Conditionals</a></h3>
  1464. <pre><pre class="playground"><code class="language-rust edition2018">
  1465. <span class="boring">#![allow(unused)]
  1466. </span><span class="boring">fn main() {
  1467. </span>// ❌ don't call hooks in conditionals!
  1468. // We must ensure that the same hooks will be called every time
  1469. // But `if` statements only run if the conditional is true!
  1470. // So we might violate rule 2.
  1471. if you_are_happy &amp;&amp; you_know_it {
  1472. let something = use_state(cx, || &quot;hands&quot;);
  1473. println!(&quot;clap your {something}&quot;)
  1474. }
  1475. // ✅ instead, *always* call use_state
  1476. // You can put other stuff in the conditional though
  1477. let something = use_state(cx, || &quot;hands&quot;);
  1478. if you_are_happy &amp;&amp; you_know_it {
  1479. println!(&quot;clap your {something}&quot;)
  1480. }
  1481. <span class="boring">}
  1482. </span></code></pre></pre>
  1483. <h3 id="no-hooks-in-closures"><a class="header" href="#no-hooks-in-closures">No Hooks in Closures</a></h3>
  1484. <pre><pre class="playground"><code class="language-rust edition2018">
  1485. <span class="boring">#![allow(unused)]
  1486. </span><span class="boring">fn main() {
  1487. </span>// ❌ don't call hooks inside closures!
  1488. // We can't guarantee that the closure, if used, will be called in the same order every time
  1489. let _a = || {
  1490. let b = use_state(cx, || 0);
  1491. b.get()
  1492. };
  1493. // ✅ instead, move hook `b` outside
  1494. let b = use_state(cx, || 0);
  1495. let _a = || b.get();
  1496. <span class="boring">}
  1497. </span></code></pre></pre>
  1498. <h3 id="no-hooks-in-loops"><a class="header" href="#no-hooks-in-loops">No Hooks in Loops</a></h3>
  1499. <pre><pre class="playground"><code class="language-rust edition2018">
  1500. <span class="boring">#![allow(unused)]
  1501. </span><span class="boring">fn main() {
  1502. </span>// `names` is a Vec&lt;&amp;str&gt;
  1503. // ❌ Do not use hooks in loops!
  1504. // In this case, if the length of the Vec changes, we break rule 2
  1505. for _name in &amp;names {
  1506. let is_selected = use_state(cx, || false);
  1507. println!(&quot;selected: {is_selected}&quot;);
  1508. }
  1509. // ✅ Instead, use a hashmap with use_ref
  1510. let selection_map = use_ref(cx, HashMap::&lt;&amp;str, bool&gt;::new);
  1511. for name in &amp;names {
  1512. let is_selected = selection_map.read()[name];
  1513. println!(&quot;selected: {is_selected}&quot;);
  1514. }
  1515. <span class="boring">}
  1516. </span></code></pre></pre>
  1517. <h2 id="use_ref-hook"><a class="header" href="#use_ref-hook"><code>use_ref</code> Hook</a></h2>
  1518. <p><code>use_state</code> is great for tracking simple values. However, you may notice in the <a href="https://docs.rs/dioxus/latest/dioxus/hooks/struct.UseState.html"><code>UseState</code> API</a> that the only way to modify its value is to replace it with something else (e.g., by calling <code>set</code>, or through one of the <code>+=</code>, <code>-=</code> operators). This works well when it is cheap to construct a value (such as any primitive). But what if you want to maintain more complex data in the components state?</p>
  1519. <p>For example, suppose we want to maintain a <code>Vec</code> of values. If we stored it with <code>use_state</code>, the only way to add a new value to the list would be to create a new <code>Vec</code> with the additional value, and put it in the state. This is expensive! We want to modify the existing <code>Vec</code> instead.</p>
  1520. <p>Thankfully, there is another hook for that, <code>use_ref</code>! It is similar to <code>use_state</code>, but it lets you get a mutable reference to the contained data.</p>
  1521. <p>Here's a simple example that keeps a list of events in a <code>use_ref</code>. We can acquire write access to the state with <code>.with_mut()</code>, and then just <code>.push</code> a new value to the state:</p>
  1522. <pre><pre class="playground"><code class="language-rust edition2018">
  1523. <span class="boring">#![allow(unused)]
  1524. </span><span class="boring">fn main() {
  1525. </span>fn App(cx: Scope) -&gt; Element {
  1526. let list = use_ref(cx, Vec::new);
  1527. cx.render(rsx!(
  1528. p { &quot;Current list: {list.read():?}&quot; }
  1529. button {
  1530. onclick: move |event| {
  1531. list.with_mut(|list| list.push(event));
  1532. },
  1533. &quot;Click me!&quot;
  1534. }
  1535. ))
  1536. }
  1537. <span class="boring">}
  1538. </span></code></pre></pre>
  1539. <blockquote>
  1540. <p>The return values of <code>use_state</code> and <code>use_ref</code> (<code>UseState</code> and <code>UseRef</code>, respectively) are in some ways similar to <a href="https://doc.rust-lang.org/std/cell/"><code>Cell</code></a> and <a href="https://doc.rust-lang.org/std/cell/struct.RefCell.html"><code>RefCell</code></a> – they provide interior mutability. However, these Dioxus wrappers also ensure that the component gets re-rendered whenever you change the state.</p>
  1541. </blockquote>
  1542. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-input"><a class="header" href="#user-input">User Input</a></h1>
  1543. <p>Interfaces often need to provide a way to input data: e.g. text, numbers, checkboxes, etc. In Dioxus, there are two ways you can work with user input.</p>
  1544. <h2 id="controlled-inputs"><a class="header" href="#controlled-inputs">Controlled Inputs</a></h2>
  1545. <p>With controlled inputs, you are directly in charge of the state of the input. This gives you a lot of flexibility, and makes it easy to keep things in sync. For example, this is how you would create a controlled text input:</p>
  1546. <pre><pre class="playground"><code class="language-rust edition2018">
  1547. <span class="boring">#![allow(unused)]
  1548. </span><span class="boring">fn main() {
  1549. </span>fn App(cx: Scope) -&gt; Element {
  1550. let name = use_state(cx, || &quot;bob&quot;.to_string());
  1551. cx.render(rsx! {
  1552. input {
  1553. // we tell the component what to render
  1554. value: &quot;{name}&quot;,
  1555. // and what to do when the value changes
  1556. oninput: move |evt| name.set(evt.value.clone()),
  1557. }
  1558. })
  1559. }
  1560. <span class="boring">}
  1561. </span></code></pre></pre>
  1562. <p>Notice the flexibility – you can:</p>
  1563. <ul>
  1564. <li>Also display the same contents in another element, and they will be in sync</li>
  1565. <li>Transform the input every time it is modified (e.g. to make sure it is upper case)</li>
  1566. <li>Validate the input every time it changes</li>
  1567. <li>Have custom logic happening when the input changes (e.g. network request for autocompletion)</li>
  1568. <li>Programmatically change the value (e.g. a &quot;randomize&quot; button that fills the input with nonsense)</li>
  1569. </ul>
  1570. <h2 id="uncontrolled-inputs"><a class="header" href="#uncontrolled-inputs">Uncontrolled Inputs</a></h2>
  1571. <p>As an alternative to controlled inputs, you can simply let the platform keep track of the input values. If we don't tell a HTML input what content it should have, it will be editable anyway (this is built into the browser). This approach can be more performant, but less flexible. For example, it's harder to keep the input in sync with another element.</p>
  1572. <p>Since you don't necessarily have the current value of the uncontrolled input in state, you can access it either by listening to <code>oninput</code> events (similarly to controlled components), or, if the input is part of a form, you can access the form data in the form events (e.g. <code>oninput</code> or <code>onsubmit</code>):</p>
  1573. <pre><pre class="playground"><code class="language-rust edition2018">
  1574. <span class="boring">#![allow(unused)]
  1575. </span><span class="boring">fn main() {
  1576. </span>fn App(cx: Scope) -&gt; Element {
  1577. cx.render(rsx! {
  1578. form {
  1579. onsubmit: move |event| {
  1580. println!(&quot;Submitted! {event:?}&quot;)
  1581. },
  1582. input { name: &quot;name&quot;, },
  1583. input { name: &quot;age&quot;, },
  1584. input { name: &quot;date&quot;, },
  1585. input { r#type: &quot;submit&quot;, },
  1586. }
  1587. })
  1588. }
  1589. <span class="boring">}
  1590. </span></code></pre></pre>
  1591. <pre><code>Submitted! UiEvent { data: FormData { value: &quot;&quot;, values: {&quot;age&quot;: &quot;very old&quot;, &quot;date&quot;: &quot;1966&quot;, &quot;name&quot;: &quot;Fred&quot;} } }
  1592. </code></pre>
  1593. <div style="break-before: page; page-break-before: always;"></div><h1 id="sharing-state"><a class="header" href="#sharing-state">Sharing State</a></h1>
  1594. <p>Often, multiple components need to access the same state. Depending on your needs, there are several ways to implement this.</p>
  1595. <h2 id="lifting-state"><a class="header" href="#lifting-state">Lifting State</a></h2>
  1596. <p>One approach to share state between components is to &quot;lift&quot; it up to the nearest common ancestor. This means putting the <code>use_state</code> hook in a parent component, and passing the needed values down as props.</p>
  1597. <p>Suppose we want to build a meme editor. We want to have an input to edit the meme caption, but also a preview of the meme with the caption. Logically, the meme and the input are 2 separate components, but they need access to the same state (the current caption).</p>
  1598. <blockquote>
  1599. <p>Of course, in this simple example, we could write everything in one component – but it is better to split everything out in smaller components to make the code more reusable, maintainable, and performant (this is even more important for larger, complex apps).</p>
  1600. </blockquote>
  1601. <p>We start with a <code>Meme</code> component, responsible for rendering a meme with a given caption:</p>
  1602. <pre><pre class="playground"><code class="language-rust edition2018">
  1603. <span class="boring">#![allow(unused)]
  1604. </span><span class="boring">fn main() {
  1605. </span>#[inline_props]
  1606. fn Meme&lt;'a&gt;(cx: Scope&lt;'a&gt;, caption: &amp;'a str) -&gt; Element&lt;'a&gt; {
  1607. let container_style = r#&quot;
  1608. position: relative;
  1609. width: fit-content;
  1610. &quot;#;
  1611. let caption_container_style = r#&quot;
  1612. position: absolute;
  1613. bottom: 0;
  1614. left: 0;
  1615. right: 0;
  1616. padding: 16px 8px;
  1617. &quot;#;
  1618. let caption_style = r&quot;
  1619. font-size: 32px;
  1620. margin: 0;
  1621. color: white;
  1622. text-align: center;
  1623. &quot;;
  1624. cx.render(rsx!(
  1625. div {
  1626. style: &quot;{container_style}&quot;,
  1627. img {
  1628. src: &quot;https://i.imgflip.com/2zh47r.jpg&quot;,
  1629. height: &quot;500px&quot;,
  1630. },
  1631. div {
  1632. style: &quot;{caption_container_style}&quot;,
  1633. p {
  1634. style: &quot;{caption_style}&quot;,
  1635. &quot;{caption}&quot;
  1636. }
  1637. }
  1638. }
  1639. ))
  1640. }
  1641. <span class="boring">}
  1642. </span></code></pre></pre>
  1643. <blockquote>
  1644. <p>Note that the <code>Meme</code> component is unaware where the caption is coming from – it could be stored in <code>use_state</code>, <code>use_ref</code>, or a constant. This ensures that it is very reusable – the same component can be used for a meme gallery without any changes!</p>
  1645. </blockquote>
  1646. <p>We also create a caption editor, completely decoupled from the meme. The caption editor must not store the caption itself – otherwise, how will we provide it to the <code>Meme</code> component? Instead, it should accept the current caption as a prop, as well as an event handler to delegate input events to:</p>
  1647. <pre><pre class="playground"><code class="language-rust edition2018">
  1648. <span class="boring">#![allow(unused)]
  1649. </span><span class="boring">fn main() {
  1650. </span>#[inline_props]
  1651. fn CaptionEditor&lt;'a&gt;(
  1652. cx: Scope&lt;'a&gt;,
  1653. caption: &amp;'a str,
  1654. on_input: EventHandler&lt;'a, FormEvent&gt;,
  1655. ) -&gt; Element&lt;'a&gt; {
  1656. let input_style = r&quot;
  1657. border: none;
  1658. background: cornflowerblue;
  1659. padding: 8px 16px;
  1660. margin: 0;
  1661. border-radius: 4px;
  1662. color: white;
  1663. &quot;;
  1664. cx.render(rsx!(input {
  1665. style: &quot;{input_style}&quot;,
  1666. value: &quot;{caption}&quot;,
  1667. oninput: move |event| on_input.call(event),
  1668. }))
  1669. }
  1670. <span class="boring">}
  1671. </span></code></pre></pre>
  1672. <p>Finally, a third component will render the other two as children. It will be responsible for keeping the state and passing down the relevant props.</p>
  1673. <pre><pre class="playground"><code class="language-rust edition2018">
  1674. <span class="boring">#![allow(unused)]
  1675. </span><span class="boring">fn main() {
  1676. </span>fn MemeEditor(cx: Scope) -&gt; Element {
  1677. let container_style = r&quot;
  1678. display: flex;
  1679. flex-direction: column;
  1680. gap: 16px;
  1681. margin: 0 auto;
  1682. width: fit-content;
  1683. &quot;;
  1684. let caption = use_state(cx, || &quot;me waiting for my rust code to compile&quot;.to_string());
  1685. cx.render(rsx! {
  1686. div {
  1687. style: &quot;{container_style}&quot;,
  1688. h1 { &quot;Meme Editor&quot; },
  1689. Meme {
  1690. caption: caption,
  1691. },
  1692. CaptionEditor {
  1693. caption: caption,
  1694. on_input: move |event: FormEvent| {caption.set(event.value.clone());},
  1695. },
  1696. }
  1697. })
  1698. }
  1699. <span class="boring">}
  1700. </span></code></pre></pre>
  1701. <p><img src="interactivity/./images/meme_editor_screenshot.png" alt="Meme Editor Screenshot: An old plastic skeleton sitting on a park bench. Caption: &quot;me waiting for a language feature&quot;" /></p>
  1702. <h2 id="using-context"><a class="header" href="#using-context">Using Context</a></h2>
  1703. <p>Sometimes, some state needs to be shared between multiple components far down the tree, and passing it down through props is very inconvenient.</p>
  1704. <p>Suppose now that we want to implement a dark mode toggle for our app. To achieve this, we will make every component select styling depending on whether dark mode is enabled or not.</p>
  1705. <blockquote>
  1706. <p>Note: we're choosing this approach for the sake of an example. There are better ways to implement dark mode (e.g. using CSS variables). Let's pretend CSS variables don't exist – welcome to 2013!</p>
  1707. </blockquote>
  1708. <p>Now, we could write another <code>use_state</code> in the top component, and pass <code>is_dark_mode</code> down to every component through props. But think about what will happen as the app grows in complexity – almost every component that renders any CSS is going to need to know if dark mode is enabled or not – so they'll all need the same dark mode prop. And every parent component will need to pass it down to them. Imagine how messy and verbose that would get, especially if we had components several levels deep!</p>
  1709. <p>Dioxus offers a better solution than this &quot;prop drilling&quot; – providing context. The <a href="https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_context_provider.html"><code>use_context_provider</code></a> hook is similar to <code>use_ref</code>, but it makes it available through <a href="https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_context.html"><code>use_context</code></a> for all children components.</p>
  1710. <p>First, we have to create a struct for our dark mode configuration:</p>
  1711. <pre><pre class="playground"><code class="language-rust edition2018">
  1712. <span class="boring">#![allow(unused)]
  1713. </span><span class="boring">fn main() {
  1714. </span>struct DarkMode(bool);
  1715. <span class="boring">}
  1716. </span></code></pre></pre>
  1717. <p>Now, in a top-level component (like <code>App</code>), we can provide the <code>DarkMode</code> context to all children components:</p>
  1718. <pre><pre class="playground"><code class="language-rust edition2018">
  1719. <span class="boring">#![allow(unused)]
  1720. </span><span class="boring">fn main() {
  1721. </span>use_shared_state_provider(cx, || DarkMode(false));
  1722. <span class="boring">}
  1723. </span></code></pre></pre>
  1724. <p>As a result, any child component of <code>App</code> (direct or not), can access the <code>DarkMode</code> context.</p>
  1725. <pre><pre class="playground"><code class="language-rust edition2018">
  1726. <span class="boring">#![allow(unused)]
  1727. </span><span class="boring">fn main() {
  1728. </span>let dark_mode_context = use_shared_state::&lt;DarkMode&gt;(cx);
  1729. <span class="boring">}
  1730. </span></code></pre></pre>
  1731. <blockquote>
  1732. <p><code>use_context</code> returns <code>Option&lt;UseSharedState&lt;DarkMode&gt;&gt;</code> here. If the context has been provided, the value is <code>Some(UseSharedState&lt;DarkMode&gt;)</code>, which you can call <code>.read</code> or <code>.write</code> on, similarly to <code>UseRef</code>. Otherwise, the value is <code>None</code>.</p>
  1733. </blockquote>
  1734. <p>For example, here's how we would implement the dark mode toggle, which both reads the context (to determine what color it should render) and writes to it (to toggle dark mode):</p>
  1735. <pre><pre class="playground"><code class="language-rust edition2018">
  1736. <span class="boring">#![allow(unused)]
  1737. </span><span class="boring">fn main() {
  1738. </span>pub fn DarkModeToggle(cx: Scope) -&gt; Element {
  1739. let dark_mode = use_shared_state::&lt;DarkMode&gt;(cx).unwrap();
  1740. let style = if dark_mode.read().0 {
  1741. &quot;color:white&quot;
  1742. } else {
  1743. &quot;&quot;
  1744. };
  1745. cx.render(rsx!(label {
  1746. style: &quot;{style}&quot;,
  1747. &quot;Dark Mode&quot;,
  1748. input {
  1749. r#type: &quot;checkbox&quot;,
  1750. oninput: move |event| {
  1751. let is_enabled = event.value == &quot;true&quot;;
  1752. dark_mode.write().0 = is_enabled;
  1753. },
  1754. },
  1755. }))
  1756. }
  1757. <span class="boring">}
  1758. </span></code></pre></pre>
  1759. <div style="break-before: page; page-break-before: always;"></div><h1 id="custom-hooks"><a class="header" href="#custom-hooks">Custom Hooks</a></h1>
  1760. <p>Hooks are a great way to encapsulate business logic. If none of the existing hooks work for your problem, you can write your own.</p>
  1761. <p>When writing your hook, you can make a function that accepts <code>cx: &amp;ScopeState</code> as a parameter to accept a scope with any Props.</p>
  1762. <h2 id="composing-hooks"><a class="header" href="#composing-hooks">Composing Hooks</a></h2>
  1763. <p>To avoid repetition, you can encapsulate business logic based on existing hooks to create a new hook.</p>
  1764. <p>For example, if many components need to access an <code>AppSettings</code> struct, you can create a &quot;shortcut&quot; hook:</p>
  1765. <pre><pre class="playground"><code class="language-rust edition2018">
  1766. <span class="boring">#![allow(unused)]
  1767. </span><span class="boring">fn main() {
  1768. </span>fn use_settings(cx: &amp;ScopeState) -&gt; &amp;UseSharedState&lt;AppSettings&gt; {
  1769. use_shared_state::&lt;AppSettings&gt;(cx).expect(&quot;App settings not provided&quot;)
  1770. }
  1771. <span class="boring">}
  1772. </span></code></pre></pre>
  1773. <p>Or if you want to wrap a hook that persists reloads with the storage API, you can build on top of the use_ref hook to work with mutable state:</p>
  1774. <pre><pre class="playground"><code class="language-rust edition2018">
  1775. <span class="boring">#![allow(unused)]
  1776. </span><span class="boring">fn main() {
  1777. </span>use gloo_storage::{LocalStorage, Storage};
  1778. use serde::{de::DeserializeOwned, Serialize};
  1779. /// A persistent storage hook that can be used to store data across application reloads.
  1780. #[allow(clippy::needless_return)]
  1781. pub fn use_persistent&lt;T: Serialize + DeserializeOwned + Default + 'static&gt;(
  1782. cx: &amp;ScopeState,
  1783. // A unique key for the storage entry
  1784. key: impl ToString,
  1785. // A function that returns the initial value if the storage entry is empty
  1786. init: impl FnOnce() -&gt; T,
  1787. ) -&gt; &amp;UsePersistent&lt;T&gt; {
  1788. // Use the use_ref hook to create a mutable state for the storage entry
  1789. let state = use_ref(cx, move || {
  1790. // This closure will run when the hook is created
  1791. let key = key.to_string();
  1792. let value = LocalStorage::get(key.as_str()).ok().unwrap_or_else(init);
  1793. StorageEntry { key, value }
  1794. });
  1795. // Wrap the state in a new struct with a custom API
  1796. // Note: We use use_hook here so that this hook is easier to use in closures in the rsx. Any values with the same lifetime as the ScopeState can be used in the closure without cloning.
  1797. cx.use_hook(|| UsePersistent {
  1798. inner: state.clone(),
  1799. })
  1800. }
  1801. struct StorageEntry&lt;T&gt; {
  1802. key: String,
  1803. value: T,
  1804. }
  1805. /// Storage that persists across application reloads
  1806. pub struct UsePersistent&lt;T: 'static&gt; {
  1807. inner: UseRef&lt;StorageEntry&lt;T&gt;&gt;,
  1808. }
  1809. impl&lt;T: Serialize + DeserializeOwned + Clone + 'static&gt; UsePersistent&lt;T&gt; {
  1810. /// Returns a reference to the value
  1811. pub fn get(&amp;self) -&gt; T {
  1812. self.inner.read().value.clone()
  1813. }
  1814. /// Sets the value
  1815. pub fn set(&amp;self, value: T) {
  1816. let mut inner = self.inner.write();
  1817. // Write the new value to local storage
  1818. LocalStorage::set(inner.key.as_str(), &amp;value);
  1819. inner.value = value;
  1820. }
  1821. }
  1822. <span class="boring">}
  1823. </span></code></pre></pre>
  1824. <h2 id="custom-hook-logic"><a class="header" href="#custom-hook-logic">Custom Hook Logic</a></h2>
  1825. <p>You can use <a href="https://docs.rs/dioxus/latest/dioxus/prelude/struct.ScopeState.html#method.use_hook"><code>cx.use_hook</code></a> to build your own hooks. In fact, this is what all the standard hooks are built on!</p>
  1826. <p><code>use_hook</code> accepts a single closure for initializing the hook. It will be only run the first time the component is rendered. The return value of that closure will be used as the value of the hook – Dioxus will take it, and store it for as long as the component is alive. On every render (not just the first one!), you will get a reference to this value.</p>
  1827. <blockquote>
  1828. <p>Note: You can implement <a href="https://doc.rust-lang.org/std/ops/trait.Drop.html"><code>Drop</code></a> for your hook value – it will be dropped then the component is unmounted (no longer in the UI)</p>
  1829. </blockquote>
  1830. <p>Inside the initialization closure, you will typically make calls to other <code>cx</code> methods. For example:</p>
  1831. <ul>
  1832. <li>The <code>use_state</code> hook tracks state in the hook value, and uses <a href="https://docs.rs/dioxus/latest/dioxus/prelude/struct.ScopeState.html#method.schedule_update"><code>cx.schedule_update</code></a> to make Dioxus re-render the component whenever it changes.</li>
  1833. </ul>
  1834. <p>Here is a simplified implementation of the <code>use_state</code> hook:</p>
  1835. <pre><pre class="playground"><code class="language-rust edition2018">
  1836. <span class="boring">#![allow(unused)]
  1837. </span><span class="boring">fn main() {
  1838. </span>use std::cell::RefCell;
  1839. use std::rc::Rc;
  1840. use std::sync::Arc;
  1841. #[derive(Clone)]
  1842. struct UseState&lt;T&gt; {
  1843. value: Rc&lt;RefCell&lt;T&gt;&gt;,
  1844. update: Arc&lt;dyn Fn()&gt;,
  1845. }
  1846. fn my_use_state&lt;T: 'static&gt;(cx: &amp;ScopeState, init: impl FnOnce() -&gt; T) -&gt; &amp;UseState&lt;T&gt; {
  1847. cx.use_hook(|| {
  1848. // The update function will trigger a re-render in the component cx is attached to
  1849. let update = cx.schedule_update();
  1850. // Create the initial state
  1851. let value = Rc::new(RefCell::new(init()));
  1852. UseState { value, update }
  1853. })
  1854. }
  1855. impl&lt;T: Clone&gt; UseState&lt;T&gt; {
  1856. fn get(&amp;self) -&gt; T {
  1857. self.value.borrow().clone()
  1858. }
  1859. fn set(&amp;self, value: T) {
  1860. // Update the state
  1861. *self.value.borrow_mut() = value;
  1862. // Trigger a re-render on the component the state is from
  1863. (self.update)();
  1864. }
  1865. }
  1866. <span class="boring">}
  1867. </span></code></pre></pre>
  1868. <ul>
  1869. <li>The <code>use_context</code> hook calls <a href="https://docs.rs/dioxus/latest/dioxus/prelude/struct.ScopeState.html#method.consume_context"><code>cx.consume_context</code></a> (which would be expensive to call on every render) to get some context from the scope</li>
  1870. </ul>
  1871. <p>Here is an implementation of the <code>use_context</code> and <code>use_context_provider</code> hooks:</p>
  1872. <pre><pre class="playground"><code class="language-rust edition2018">
  1873. <span class="boring">#![allow(unused)]
  1874. </span><span class="boring">fn main() {
  1875. </span>pub fn use_context&lt;T: 'static + Clone&gt;(cx: &amp;ScopeState) -&gt; Option&lt;&amp;T&gt; {
  1876. cx.use_hook(|| cx.consume_context::&lt;T&gt;()).as_ref()
  1877. }
  1878. pub fn use_context_provider&lt;T: 'static + Clone&gt;(cx: &amp;ScopeState, f: impl FnOnce() -&gt; T) -&gt; &amp;T {
  1879. cx.use_hook(|| {
  1880. let val = f();
  1881. // Provide the context state to the scope
  1882. cx.provide_context(val.clone());
  1883. val
  1884. })
  1885. }
  1886. <span class="boring">}
  1887. </span></code></pre></pre>
  1888. <h2 id="hook-anti-patterns"><a class="header" href="#hook-anti-patterns">Hook Anti-Patterns</a></h2>
  1889. <p>When writing a custom hook, you should avoid the following anti-patterns:</p>
  1890. <ul>
  1891. <li>!Clone Hooks: To allow hooks to be used within async blocks, the hooks must be Clone. To make a hook clone, you can wrap data in Rc or Arc and avoid lifetimes in hooks.</li>
  1892. </ul>
  1893. <p>This version of use_state may seem more efficient, but it is not cloneable:</p>
  1894. <pre><pre class="playground"><code class="language-rust edition2018">
  1895. <span class="boring">#![allow(unused)]
  1896. </span><span class="boring">fn main() {
  1897. </span>use std::cell::RefCell;
  1898. use std::rc::Rc;
  1899. use std::sync::Arc;
  1900. struct UseState&lt;'a, T&gt; {
  1901. value: &amp;'a RefCell&lt;T&gt;,
  1902. update: Arc&lt;dyn Fn()&gt;,
  1903. }
  1904. fn my_use_state&lt;T: 'static&gt;(cx: &amp;ScopeState, init: impl FnOnce() -&gt; T) -&gt; UseState&lt;T&gt; {
  1905. // The update function will trigger a re-render in the component cx is attached to
  1906. let update = cx.schedule_update();
  1907. // Create the initial state
  1908. let value = cx.use_hook(|| RefCell::new(init()));
  1909. UseState { value, update }
  1910. }
  1911. impl&lt;T: Clone&gt; UseState&lt;'_, T&gt; {
  1912. fn get(&amp;self) -&gt; T {
  1913. self.value.borrow().clone()
  1914. }
  1915. fn set(&amp;self, value: T) {
  1916. // Update the state
  1917. *self.value.borrow_mut() = value;
  1918. // Trigger a re-render on the component the state is from
  1919. (self.update)();
  1920. }
  1921. }
  1922. <span class="boring">}
  1923. </span></code></pre></pre>
  1924. <p>If we try to use this hook in an async block, we will get a compile error:</p>
  1925. <pre><pre class="playground"><code class="language-rust edition2018">
  1926. <span class="boring">#![allow(unused)]
  1927. </span><span class="boring">fn main() {
  1928. </span>fn FutureComponent(cx: &amp;ScopeState) -&gt; Element {
  1929. let my_state = my_use_state(cx, || 0);
  1930. cx.spawn({
  1931. to_owned![my_state];
  1932. async move {
  1933. my_state.set(1);
  1934. }
  1935. });
  1936. todo!()
  1937. }
  1938. <span class="boring">}
  1939. </span></code></pre></pre>
  1940. <p>But with the original version, we can use it in an async block:</p>
  1941. <pre><pre class="playground"><code class="language-rust edition2018">
  1942. <span class="boring">#![allow(unused)]
  1943. </span><span class="boring">fn main() {
  1944. </span>fn FutureComponent(cx: &amp;ScopeState) -&gt; Element {
  1945. let my_state = use_state(cx, || 0);
  1946. cx.spawn({
  1947. to_owned![my_state];
  1948. async move {
  1949. my_state.set(1);
  1950. }
  1951. });
  1952. todo!()
  1953. }
  1954. <span class="boring">}
  1955. </span></code></pre></pre>
  1956. <div style="break-before: page; page-break-before: always;"></div><h1 id="dynamic-rendering"><a class="header" href="#dynamic-rendering">Dynamic Rendering</a></h1>
  1957. <p>Sometimes you want to render different things depending on the state/props. With Dioxus, just describe what you want to see using Rust control flow – the framework will take care of making the necessary changes on the fly if the state or props change!</p>
  1958. <h2 id="conditional-rendering"><a class="header" href="#conditional-rendering">Conditional Rendering</a></h2>
  1959. <p>To render different elements based on a condition, you could use an <code>if-else</code> statement:</p>
  1960. <pre><pre class="playground"><code class="language-rust edition2018">
  1961. <span class="boring">#![allow(unused)]
  1962. </span><span class="boring">fn main() {
  1963. </span>if *is_logged_in {
  1964. cx.render(rsx! {
  1965. &quot;Welcome!&quot;
  1966. button {
  1967. onclick: move |_| on_log_out.call(()),
  1968. &quot;Log Out&quot;,
  1969. }
  1970. })
  1971. } else {
  1972. cx.render(rsx! {
  1973. button {
  1974. onclick: move |_| on_log_in.call(()),
  1975. &quot;Log In&quot;,
  1976. }
  1977. })
  1978. }
  1979. <span class="boring">}
  1980. </span></code></pre></pre>
  1981. <blockquote>
  1982. <p>You could also use <code>match</code> statements, or any Rust function to conditionally render different things.</p>
  1983. </blockquote>
  1984. <h3 id="improving-the-if-else-example"><a class="header" href="#improving-the-if-else-example">Improving the <code>if-else</code> Example</a></h3>
  1985. <p>You may have noticed some repeated code in the <code>if-else</code> example above. Repeating code like this is both bad for maintainability and performance. Dioxus will skip diffing static elements like the button, but when switching between multiple <code>rsx</code> calls it cannot perform this optimization. For this example either approach is fine, but for components with large parts that are reused between conditionals, it can be more of an issue.</p>
  1986. <p>We can improve this example by splitting up the dynamic parts and inserting them where they are needed.</p>
  1987. <pre><pre class="playground"><code class="language-rust edition2018">
  1988. <span class="boring">#![allow(unused)]
  1989. </span><span class="boring">fn main() {
  1990. </span>cx.render(rsx! {
  1991. // We only render the welcome message if we are logged in
  1992. // You can use if statements in the middle of a render block to conditionally render elements
  1993. if *is_logged_in {
  1994. // Notice the body of this if statment is rsx code, not an expression
  1995. &quot;Welcome!&quot;
  1996. }
  1997. button {
  1998. // depending on the value of `is_logged_in`, we will call a different event handler
  1999. onclick: move |_| if *is_logged_in {
  2000. on_log_in.call(())
  2001. }
  2002. else{
  2003. on_log_out.call(())
  2004. },
  2005. if *is_logged_in {
  2006. // if we are logged in, the button should say &quot;Log Out&quot;
  2007. &quot;Log Out&quot;
  2008. } else {
  2009. // if we are not logged in, the button should say &quot;Log In&quot;
  2010. &quot;Log In&quot;
  2011. }
  2012. }
  2013. })
  2014. <span class="boring">}
  2015. </span></code></pre></pre>
  2016. <h3 id="inspecting-element-props"><a class="header" href="#inspecting-element-props">Inspecting <code>Element</code> props</a></h3>
  2017. <p>Since <code>Element</code> is a <code>Option&lt;VNode&gt;</code>, components accepting <code>Element</code> as a prop can inspect its contents, and render different things based on that. Example:</p>
  2018. <pre><pre class="playground"><code class="language-rust edition2018">
  2019. <span class="boring">#![allow(unused)]
  2020. </span><span class="boring">fn main() {
  2021. </span>fn Clickable&lt;'a&gt;(cx: Scope&lt;'a, ClickableProps&lt;'a&gt;&gt;) -&gt; Element {
  2022. match cx.props.children {
  2023. Some(VNode { dynamic_nodes, .. }) =&gt; {
  2024. todo!(&quot;render some stuff&quot;)
  2025. }
  2026. _ =&gt; {
  2027. todo!(&quot;render some other stuff&quot;)
  2028. }
  2029. }
  2030. }
  2031. <span class="boring">}
  2032. </span></code></pre></pre>
  2033. <p>You can't mutate the <code>Element</code>, but if you need a modified version of it, you can construct a new one based on its attributes/children/etc.</p>
  2034. <h2 id="rendering-nothing"><a class="header" href="#rendering-nothing">Rendering Nothing</a></h2>
  2035. <p>To render nothing, you can return <code>None</code> from a component. This is useful if you want to conditionally hide something:</p>
  2036. <pre><pre class="playground"><code class="language-rust edition2018">
  2037. <span class="boring">#![allow(unused)]
  2038. </span><span class="boring">fn main() {
  2039. </span>if *is_logged_in {
  2040. return None;
  2041. }
  2042. cx.render(rsx! {
  2043. a {
  2044. &quot;You must be logged in to comment&quot;
  2045. }
  2046. })
  2047. <span class="boring">}
  2048. </span></code></pre></pre>
  2049. <p>This works because the <code>Element</code> type is just an alias for <code>Option&lt;VNode&gt;</code></p>
  2050. <blockquote>
  2051. <p>Again, you may use a different method to conditionally return <code>None</code>. For example the boolean's <a href="https://doc.rust-lang.org/std/primitive.bool.html#method.then"><code>then()</code></a> function could be used.</p>
  2052. </blockquote>
  2053. <h2 id="rendering-lists"><a class="header" href="#rendering-lists">Rendering Lists</a></h2>
  2054. <p>Often, you'll want to render a collection of components. For example, you might want to render a list of all comments on a post.</p>
  2055. <p>For this, Dioxus accepts iterators that produce <code>Element</code>s. So we need to:</p>
  2056. <ul>
  2057. <li>Get an iterator over all of our items (e.g., if you have a <code>Vec</code> of comments, iterate over it with <code>iter()</code>)</li>
  2058. <li><code>.map</code> the iterator to convert each item into a <code>LazyNode</code> using <code>rsx!(...)</code>
  2059. <ul>
  2060. <li>Add a unique <code>key</code> attribute to each iterator item</li>
  2061. </ul>
  2062. </li>
  2063. <li>Include this iterator in the final RSX (or use it inline)</li>
  2064. </ul>
  2065. <p>Example: suppose you have a list of comments you want to render. Then, you can render them like this:</p>
  2066. <pre><pre class="playground"><code class="language-rust edition2018">
  2067. <span class="boring">#![allow(unused)]
  2068. </span><span class="boring">fn main() {
  2069. </span>let comment_field = use_state(cx, String::new);
  2070. let mut next_id = use_state(cx, || 0);
  2071. let comments = use_ref(cx, Vec::&lt;Comment&gt;::new);
  2072. let comments_lock = comments.read();
  2073. let comments_rendered = comments_lock.iter().map(|comment| {
  2074. rsx!(CommentComponent {
  2075. key: &quot;{comment.id}&quot;,
  2076. comment: comment.clone(),
  2077. })
  2078. });
  2079. cx.render(rsx!(
  2080. form {
  2081. onsubmit: move |_| {
  2082. comments.write().push(Comment {
  2083. content: comment_field.get().clone(),
  2084. id: *next_id.get(),
  2085. });
  2086. next_id += 1;
  2087. comment_field.set(String::new());
  2088. },
  2089. input {
  2090. value: &quot;{comment_field}&quot;,
  2091. oninput: |event| comment_field.set(event.value.clone()),
  2092. }
  2093. input {
  2094. r#type: &quot;submit&quot;,
  2095. }
  2096. },
  2097. comments_rendered,
  2098. ))
  2099. <span class="boring">}
  2100. </span></code></pre></pre>
  2101. <h3 id="inline-for-loops"><a class="header" href="#inline-for-loops">Inline for loops</a></h3>
  2102. <p>Because of how common it is to render a list of items, Dioxus provides a shorthand for this. Instead of using <code>.iter, </code>.map<code>, and </code>rsx<code>, you can use a </code>for` loop with a body of rsx code:</p>
  2103. <pre><pre class="playground"><code class="language-rust edition2018">
  2104. <span class="boring">#![allow(unused)]
  2105. </span><span class="boring">fn main() {
  2106. </span>let comment_field = use_state(cx, String::new);
  2107. let mut next_id = use_state(cx, || 0);
  2108. let comments = use_ref(cx, Vec::&lt;Comment&gt;::new);
  2109. cx.render(rsx!(
  2110. form {
  2111. onsubmit: move |_| {
  2112. comments.write().push(Comment {
  2113. content: comment_field.get().clone(),
  2114. id: *next_id.get(),
  2115. });
  2116. next_id += 1;
  2117. comment_field.set(String::new());
  2118. },
  2119. input {
  2120. value: &quot;{comment_field}&quot;,
  2121. oninput: |event| comment_field.set(event.value.clone()),
  2122. }
  2123. input {
  2124. r#type: &quot;submit&quot;,
  2125. }
  2126. },
  2127. for comment in &amp;*comments.read() {
  2128. // Notice the body of this for loop is rsx code, not an expression
  2129. CommentComponent {
  2130. key: &quot;{comment.id}&quot;,
  2131. comment: comment.clone(),
  2132. }
  2133. }
  2134. ))
  2135. <span class="boring">}
  2136. </span></code></pre></pre>
  2137. <h3 id="the-key-attribute"><a class="header" href="#the-key-attribute">The <code>key</code> Attribute</a></h3>
  2138. <p>Every time you re-render your list, Dioxus needs to keep track of which items go where to determine what updates need to be made to the UI.</p>
  2139. <p>For example, suppose the <code>CommentComponent</code> had some state – e.g. a field where the user typed in a reply. If the order of comments suddenly changes, Dioxus needs to correctly associate that state with the same comment – otherwise, the user will end up replying to a different comment!</p>
  2140. <p>To help Dioxus keep track of list items, we need to associate each item with a unique key. In the example above, we dynamically generated the unique key. In real applications, it's more likely that the key will come from e.g. a database ID. It doesn't matter where you get the key from, as long as it meets the requirements:</p>
  2141. <ul>
  2142. <li>Keys must be unique in a list</li>
  2143. <li>The same item should always get associated with the same key</li>
  2144. <li>Keys should be relatively small (i.e. converting the entire Comment structure to a String would be a pretty bad key) so they can be compared efficiently</li>
  2145. </ul>
  2146. <p>You might be tempted to use an item's index in the list as its key. That’s what Dioxus will use if you don’t specify a key at all. This is only acceptable if you can guarantee that the list is constant – i.e., no re-ordering, additions, or deletions.</p>
  2147. <blockquote>
  2148. <p>Note that if you pass the key to a component you've made, it won't receive the key as a prop. It’s only used as a hint by Dioxus itself. If your component needs an ID, you have to pass it as a separate prop.</p>
  2149. </blockquote>
  2150. <div style="break-before: page; page-break-before: always;"></div><h1 id="router"><a class="header" href="#router">Router</a></h1>
  2151. <p>In many of your apps, you'll want to have different &quot;scenes&quot;. For a webpage, these scenes might be the different webpages with their own content. For a desktop app, these scenes might be different views in your app.</p>
  2152. <p>To unify these platforms, Dioxus provides a first-party solution for scene management called Dioxus Router.</p>
  2153. <h2 id="what-is-it"><a class="header" href="#what-is-it">What is it?</a></h2>
  2154. <p>For an app like the Dioxus landing page (https://dioxuslabs.com), we want to have several different scenes:</p>
  2155. <ul>
  2156. <li>Homepage</li>
  2157. <li>Blog</li>
  2158. </ul>
  2159. <p>Each of these scenes is independent – we don't want to render both the homepage and blog at the same time.</p>
  2160. <p>The Dioxus router makes it easy to create these scenes. To make sure we're using the router, add the <code>dioxus-router</code> package to your <code>Cargo.toml</code>.</p>
  2161. <pre><code class="language-shell">cargo add dioxus-router
  2162. </code></pre>
  2163. <h2 id="using-the-router"><a class="header" href="#using-the-router">Using the router</a></h2>
  2164. <p>Unlike other routers in the Rust ecosystem, our router is built declaratively. This makes it possible to compose our app layout simply by arranging components.</p>
  2165. <pre><pre class="playground"><code class="language-rust edition2018">
  2166. <span class="boring">#![allow(unused)]
  2167. </span><span class="boring">fn main() {
  2168. </span>rsx!{
  2169. // All of our routes will be rendered inside this Router component
  2170. Router {
  2171. // if the current location is &quot;/home&quot;, render the Home component
  2172. Route { to: &quot;/home&quot;, Home {} }
  2173. // if the current location is &quot;/blog&quot;, render the Blog component
  2174. Route { to: &quot;/blog&quot;, Blog {} }
  2175. }
  2176. }
  2177. <span class="boring">}
  2178. </span></code></pre></pre>
  2179. <p>Whenever we visit this app, we will get either the Home component or the Blog component rendered depending on which route we enter at. If neither of these routes match the current location, then nothing will render.</p>
  2180. <p>We can fix this one of two ways:</p>
  2181. <ul>
  2182. <li>A fallback 404 page</li>
  2183. </ul>
  2184. <pre><pre class="playground"><code class="language-rust edition2018">
  2185. <span class="boring">#![allow(unused)]
  2186. </span><span class="boring">fn main() {
  2187. </span>rsx!{
  2188. Router {
  2189. Route { to: &quot;/home&quot;, Home {} }
  2190. Route { to: &quot;/blog&quot;, Blog {} }
  2191. // if the current location doesn't match any of the above routes, render the NotFound component
  2192. Route { to: &quot;&quot;, NotFound {} }
  2193. }
  2194. }
  2195. <span class="boring">}
  2196. </span></code></pre></pre>
  2197. <ul>
  2198. <li>Redirect 404 to home</li>
  2199. </ul>
  2200. <pre><pre class="playground"><code class="language-rust edition2018">
  2201. <span class="boring">#![allow(unused)]
  2202. </span><span class="boring">fn main() {
  2203. </span>rsx!{
  2204. Router {
  2205. Route { to: &quot;/home&quot;, Home {} }
  2206. Route { to: &quot;/blog&quot;, Blog {} }
  2207. // if the current location doesn't match any of the above routes, redirect to &quot;/home&quot;
  2208. Redirect { from: &quot;&quot;, to: &quot;/home&quot; }
  2209. }
  2210. }
  2211. <span class="boring">}
  2212. </span></code></pre></pre>
  2213. <h2 id="links"><a class="header" href="#links">Links</a></h2>
  2214. <p>For our app to navigate these routes, we can provide clickable elements called Links. These simply wrap <code>&lt;a&gt;</code> elements that, when clicked, navigate the app to the given location.</p>
  2215. <pre><pre class="playground"><code class="language-rust edition2018">
  2216. <span class="boring">#![allow(unused)]
  2217. </span><span class="boring">fn main() {
  2218. </span>rsx!{
  2219. Link {
  2220. to: &quot;/home&quot;,
  2221. &quot;Go home!&quot;
  2222. }
  2223. }
  2224. <span class="boring">}
  2225. </span></code></pre></pre>
  2226. <h2 id="more-reading"><a class="header" href="#more-reading">More reading</a></h2>
  2227. <p>This page is just a very brief overview of the router. For more information, check out <a href="https://dioxuslabs.com/docs/0.3/router/">the router book</a> or some of <a href="https://github.com/DioxusLabs/dioxus/blob/master/examples/router.rs">the router examples</a>.</p>
  2228. <div style="break-before: page; page-break-before: always;"></div><h1 id="working-with-async"><a class="header" href="#working-with-async">Working with Async</a></h1>
  2229. <p>Often, apps need to interact with file systems, network interfaces, hardware, or timers. This chapter provides an overview of using async code in Dioxus.</p>
  2230. <h2 id="the-runtime"><a class="header" href="#the-runtime">The Runtime</a></h2>
  2231. <p>By default, Dioxus-Desktop ships with the <code>Tokio</code> runtime and automatically sets everything up for you. This is currently not configurable, though it would be easy to write an integration for Dioxus desktop that uses a different asynchronous runtime.</p>
  2232. <p>Dioxus is not currently thread-safe, so any async code you write does <em>not</em> need to be <code>Send/Sync</code>. That means that you can use non-thread-safe structures like <code>Cell</code>, <code>Rc</code>, and <code>RefCell</code>.</p>
  2233. <div style="break-before: page; page-break-before: always;"></div><h1 id="usefuture"><a class="header" href="#usefuture">UseFuture</a></h1>
  2234. <p><a href="https://docs.rs/dioxus-hooks/latest/dioxus_hooks/fn.use_future.html"><code>use_future</code></a> lets you run an async closure, and provides you with its result.</p>
  2235. <p>For example, we can make an API request (using <a href="https://docs.rs/reqwest/latest/reqwest/index.html">reqwest</a>) inside <code>use_future</code>:</p>
  2236. <pre><pre class="playground"><code class="language-rust edition2018">
  2237. <span class="boring">#![allow(unused)]
  2238. </span><span class="boring">fn main() {
  2239. </span>let future = use_future(cx, (), |_| async move {
  2240. reqwest::get(&quot;https://dog.ceo/api/breeds/image/random&quot;)
  2241. .await
  2242. .unwrap()
  2243. .json::&lt;ApiResponse&gt;()
  2244. .await
  2245. });
  2246. <span class="boring">}
  2247. </span></code></pre></pre>
  2248. <p>The code inside <code>use_future</code> will be submitted to the Dioxus scheduler once the component has rendered.</p>
  2249. <p>We can use <code>.value()</code> to get the result of the future. On the first run, since there's no data ready when the component loads, its value will be <code>None</code>. However, once the future is finished, the component will be re-rendered and the value will now be <code>Some(...)</code>, containing the return value of the closure.</p>
  2250. <p>We can then render that result:</p>
  2251. <pre><pre class="playground"><code class="language-rust edition2018">
  2252. <span class="boring">#![allow(unused)]
  2253. </span><span class="boring">fn main() {
  2254. </span>cx.render(match future.value() {
  2255. Some(Ok(response)) =&gt; rsx! {
  2256. button {
  2257. onclick: move |_| future.restart(),
  2258. &quot;Click to fetch another doggo&quot;
  2259. }
  2260. div {
  2261. img {
  2262. max_width: &quot;500px&quot;,
  2263. max_height: &quot;500px&quot;,
  2264. src: &quot;{response.image_url}&quot;,
  2265. }
  2266. }
  2267. },
  2268. Some(Err(_)) =&gt; rsx! { div { &quot;Loading dogs failed&quot; } },
  2269. None =&gt; rsx! { div { &quot;Loading dogs...&quot; } },
  2270. })
  2271. <span class="boring">}
  2272. </span></code></pre></pre>
  2273. <h2 id="restarting-the-future"><a class="header" href="#restarting-the-future">Restarting the Future</a></h2>
  2274. <p>The <code>UseFuture</code> handle provides a <code>restart</code> method. It can be used to execute the future again, producing a new value.</p>
  2275. <h2 id="dependencies"><a class="header" href="#dependencies">Dependencies</a></h2>
  2276. <p>Often, you will need to run the future again every time some value (e.g. a prop) changes. Rather than calling <code>restart</code> manually, you can provide a tuple of &quot;dependencies&quot; to the hook. It will automatically re-run the future when any of those dependencies change. Example:</p>
  2277. <pre><pre class="playground"><code class="language-rust edition2018">
  2278. <span class="boring">#![allow(unused)]
  2279. </span><span class="boring">fn main() {
  2280. </span>let future = use_future(cx, (breed,), |(breed,)| async move {
  2281. reqwest::get(format!(&quot;https://dog.ceo/api/breed/{breed}/images/random&quot;))
  2282. .await
  2283. .unwrap()
  2284. .json::&lt;ApiResponse&gt;()
  2285. .await
  2286. });
  2287. <span class="boring">}
  2288. </span></code></pre></pre>
  2289. <div style="break-before: page; page-break-before: always;"></div><h1 id="coroutines"><a class="header" href="#coroutines">Coroutines</a></h1>
  2290. <p>Another tool in your async toolbox are coroutines. Coroutines are futures that can be manually stopped, started, paused, and resumed.</p>
  2291. <p>Like regular futures, code in a coroutine will run until the next <code>await</code> point before yielding. This low-level control over asynchronous tasks is quite powerful, allowing for infinitely looping tasks like WebSocket polling, background timers, and other periodic actions.</p>
  2292. <h2 id="use_coroutine"><a class="header" href="#use_coroutine"><code>use_coroutine</code></a></h2>
  2293. <p>The <code>use_coroutine</code> hook allows you to create a coroutine. Most coroutines we write will be polling loops using async/await.</p>
  2294. <pre><pre class="playground"><code class="language-rust edition2018">
  2295. <span class="boring">#![allow(unused)]
  2296. </span><span class="boring">fn main() {
  2297. </span>fn app(cx: Scope) -&gt; Element {
  2298. let ws: &amp;UseCoroutine&lt;()&gt; = use_coroutine(cx, |rx| async move {
  2299. // Connect to some sort of service
  2300. let mut conn = connect_to_ws_server().await;
  2301. // Wait for data on the service
  2302. while let Some(msg) = conn.next().await {
  2303. // handle messages
  2304. }
  2305. });
  2306. }
  2307. <span class="boring">}
  2308. </span></code></pre></pre>
  2309. <p>For many services, a simple async loop will handle the majority of use cases.</p>
  2310. <p>However, if we want to temporarily disable the coroutine, we can &quot;pause&quot; it using the <code>pause</code> method, and &quot;resume&quot; it using the <code>resume</code> method:</p>
  2311. <pre><pre class="playground"><code class="language-rust edition2018">
  2312. <span class="boring">#![allow(unused)]
  2313. </span><span class="boring">fn main() {
  2314. </span>let sync: &amp;UseCoroutine&lt;()&gt; = use_coroutine(cx, |rx| async move {
  2315. // code for syncing
  2316. });
  2317. if sync.is_running() {
  2318. cx.render(rsx!{
  2319. button {
  2320. onclick: move |_| sync.pause(),
  2321. &quot;Disable syncing&quot;
  2322. }
  2323. })
  2324. } else {
  2325. cx.render(rsx!{
  2326. button {
  2327. onclick: move |_| sync.resume(),
  2328. &quot;Enable syncing&quot;
  2329. }
  2330. })
  2331. }
  2332. <span class="boring">}
  2333. </span></code></pre></pre>
  2334. <p>This pattern is where coroutines are extremely useful – instead of writing all the complicated logic for pausing our async tasks like we would with JavaScript promises, the Rust model allows us to just not poll our future.</p>
  2335. <h2 id="yielding-values"><a class="header" href="#yielding-values">Yielding Values</a></h2>
  2336. <p>To yield values from a coroutine, simply bring in a <code>UseState</code> handle and set the value whenever your coroutine completes its work.</p>
  2337. <p>The future must be <code>'static</code> – so any values captured by the task cannot carry any references to <code>cx</code>, such as a <code>UseState</code>.</p>
  2338. <p>You can use <a href="https://doc.rust-lang.org/std/borrow/trait.ToOwned.html#tymethod.to_owned">to_owned</a> to create a clone of the hook handle which can be moved into the async closure.</p>
  2339. <pre><pre class="playground"><code class="language-rust edition2018">
  2340. <span class="boring">#![allow(unused)]
  2341. </span><span class="boring">fn main() {
  2342. </span>let sync_status = use_state(cx, || Status::Launching);
  2343. let sync_task = use_coroutine(cx, |rx: UnboundedReceiver&lt;SyncAction&gt;| {
  2344. let sync_status = sync_status.to_owned();
  2345. async move {
  2346. loop {
  2347. delay_ms(1000).await;
  2348. sync_status.set(Status::Working);
  2349. }
  2350. }
  2351. })
  2352. <span class="boring">}
  2353. </span></code></pre></pre>
  2354. <p>To make this a bit less verbose, Dioxus exports the <code>to_owned!</code> macro which will create a binding as shown above, which can be quite helpful when dealing with many values.</p>
  2355. <pre><pre class="playground"><code class="language-rust edition2018">
  2356. <span class="boring">#![allow(unused)]
  2357. </span><span class="boring">fn main() {
  2358. </span>let sync_status = use_state(cx, || Status::Launching);
  2359. let load_status = use_state(cx, || Status::Launching);
  2360. let sync_task = use_coroutine(cx, |rx: UnboundedReceiver&lt;SyncAction&gt;| {
  2361. to_owned![sync_status, load_status];
  2362. async move {
  2363. // ...
  2364. }
  2365. })
  2366. <span class="boring">}
  2367. </span></code></pre></pre>
  2368. <h2 id="sending-values"><a class="header" href="#sending-values">Sending Values</a></h2>
  2369. <p>You might've noticed the <code>use_coroutine</code> closure takes an argument called <code>rx</code>. What is that? Well, a common pattern in complex apps is to handle a bunch of async code at once. With libraries like Redux Toolkit, managing multiple promises at once can be challenging and a common source of bugs.</p>
  2370. <p>With Coroutines, we can centralize our async logic. The <code>rx</code> parameter is an Channel that allows code external to the coroutine to send data <em>into</em> the coroutine. Instead of looping on an external service, we can loop on the channel itself, processing messages from within our app without needing to spawn a new future. To send data into the coroutine, we would call &quot;send&quot; on the handle.</p>
  2371. <pre><pre class="playground"><code class="language-rust edition2018">
  2372. <span class="boring">#![allow(unused)]
  2373. </span><span class="boring">fn main() {
  2374. </span>use futures_util::stream::StreamExt;
  2375. enum ProfileUpdate {
  2376. SetUsername(String),
  2377. SetAge(i32)
  2378. }
  2379. let profile = use_coroutine(cx, |mut rx: UnboundedReciver&lt;ProfileUpdate&gt;| async move {
  2380. let mut server = connect_to_server().await;
  2381. while let Ok(msg) = rx.next().await {
  2382. match msg {
  2383. ProfileUpdate::SetUsername(name) =&gt; server.update_username(name).await,
  2384. ProfileUpdate::SetAge(age) =&gt; server.update_age(age).await,
  2385. }
  2386. }
  2387. });
  2388. cx.render(rsx!{
  2389. button {
  2390. onclick: move |_| profile.send(ProfileUpdate::SetUsername(&quot;Bob&quot;.to_string())),
  2391. &quot;Update username&quot;
  2392. }
  2393. })
  2394. <span class="boring">}
  2395. </span></code></pre></pre>
  2396. <blockquote>
  2397. <p>Note: In order to use/run the <code>rx.next().await</code> statement you will need to extend the [<code>Stream</code>] trait (used by [<code>UnboundedReceiver</code>]) by adding 'futures_util' as a dependency to your project and adding the <code>use futures_util::stream::StreamExt;</code>.</p>
  2398. </blockquote>
  2399. <p>For sufficiently complex apps, we could build a bunch of different useful &quot;services&quot; that loop on channels to update the app.</p>
  2400. <pre><pre class="playground"><code class="language-rust edition2018">
  2401. <span class="boring">#![allow(unused)]
  2402. </span><span class="boring">fn main() {
  2403. </span>let profile = use_coroutine(cx, profile_service);
  2404. let editor = use_coroutine(cx, editor_service);
  2405. let sync = use_coroutine(cx, sync_service);
  2406. async fn profile_service(rx: UnboundedReceiver&lt;ProfileCommand&gt;) {
  2407. // do stuff
  2408. }
  2409. async fn sync_service(rx: UnboundedReceiver&lt;SyncCommand&gt;) {
  2410. // do stuff
  2411. }
  2412. async fn editor_service(rx: UnboundedReceiver&lt;EditorCommand&gt;) {
  2413. // do stuff
  2414. }
  2415. <span class="boring">}
  2416. </span></code></pre></pre>
  2417. <p>We can combine coroutines with <a href="https://docs.rs/fermi/latest/fermi/index.html">Fermi</a> to emulate Redux Toolkit's Thunk system with much less headache. This lets us store all of our app's state <em>within</em> a task and then simply update the &quot;view&quot; values stored in Atoms. It cannot be understated how powerful this technique is: we get all the perks of native Rust tasks with the optimizations and ergonomics of global state. This means your <em>actual</em> state does not need to be tied up in a system like Fermi or Redux – the only Atoms that need to exist are those that are used to drive the display/UI.</p>
  2418. <pre><pre class="playground"><code class="language-rust edition2018">
  2419. <span class="boring">#![allow(unused)]
  2420. </span><span class="boring">fn main() {
  2421. </span>static USERNAME: Atom&lt;String&gt; = |_| &quot;default&quot;.to_string();
  2422. fn app(cx: Scope) -&gt; Element {
  2423. let atoms = use_atom_root(cx);
  2424. use_coroutine(cx, |rx| sync_service(rx, atoms.clone()));
  2425. cx.render(rsx!{
  2426. Banner {}
  2427. })
  2428. }
  2429. fn Banner(cx: Scope) -&gt; Element {
  2430. let username = use_read(cx, USERNAME);
  2431. cx.render(rsx!{
  2432. h1 { &quot;Welcome back, {username}&quot; }
  2433. })
  2434. }
  2435. <span class="boring">}
  2436. </span></code></pre></pre>
  2437. <p>Now, in our sync service, we can structure our state however we want. We only need to update the view values when ready.</p>
  2438. <pre><pre class="playground"><code class="language-rust edition2018">
  2439. <span class="boring">#![allow(unused)]
  2440. </span><span class="boring">fn main() {
  2441. </span>use futures_util::stream::StreamExt;
  2442. enum SyncAction {
  2443. SetUsername(String),
  2444. }
  2445. async fn sync_service(mut rx: UnboundedReceiver&lt;SyncAction&gt;, atoms: AtomRoot) {
  2446. let username = atoms.write(USERNAME);
  2447. let errors = atoms.write(ERRORS);
  2448. while let Ok(msg) = rx.next().await {
  2449. match msg {
  2450. SyncAction::SetUsername(name) =&gt; {
  2451. if set_name_on_server(&amp;name).await.is_ok() {
  2452. username.set(name);
  2453. } else {
  2454. errors.make_mut().push(&quot;SetUsernameFailed&quot;);
  2455. }
  2456. }
  2457. }
  2458. }
  2459. }
  2460. <span class="boring">}
  2461. </span></code></pre></pre>
  2462. <h2 id="automatic-injection-into-the-context-api"><a class="header" href="#automatic-injection-into-the-context-api">Automatic injection into the Context API</a></h2>
  2463. <p>Coroutine handles are automatically injected through the context API. You can use the <code>use_coroutine_handle</code> hook with the message type as a generic to fetch a handle.</p>
  2464. <pre><pre class="playground"><code class="language-rust edition2018">
  2465. <span class="boring">#![allow(unused)]
  2466. </span><span class="boring">fn main() {
  2467. </span>fn Child(cx: Scope) -&gt; Element {
  2468. let sync_task = use_coroutine_handle::&lt;SyncAction&gt;(cx);
  2469. sync_task.send(SyncAction::SetUsername);
  2470. }
  2471. <span class="boring">}
  2472. </span></code></pre></pre>
  2473. <div style="break-before: page; page-break-before: always;"></div><h1 id="spawning-futures"><a class="header" href="#spawning-futures">Spawning Futures</a></h1>
  2474. <p>The <code>use_future</code> and <code>use_coroutine</code> hooks are useful if you want to unconditionally spawn the future. Sometimes, though, you'll want to only spawn a future in response to an event, such as a mouse click. For example, suppose you need to send a request when the user clicks a &quot;log in&quot; button. For this, you can use <code>cx.spawn</code>:</p>
  2475. <pre><pre class="playground"><code class="language-rust edition2018">
  2476. <span class="boring">#![allow(unused)]
  2477. </span><span class="boring">fn main() {
  2478. </span> let logged_in = use_state(cx, || false);
  2479. let log_in = move |_| {
  2480. cx.spawn({
  2481. let logged_in = logged_in.to_owned();
  2482. async move {
  2483. let resp = reqwest::Client::new()
  2484. .post(&quot;http://example.com/login&quot;)
  2485. .send()
  2486. .await;
  2487. match resp {
  2488. Ok(_data) =&gt; {
  2489. println!(&quot;Login successful!&quot;);
  2490. logged_in.set(true);
  2491. }
  2492. Err(_err) =&gt; {
  2493. println!(
  2494. &quot;Login failed - you need a login server running on localhost:8080.&quot;
  2495. )
  2496. }
  2497. }
  2498. }
  2499. });
  2500. };
  2501. cx.render(rsx! {
  2502. button {
  2503. onclick: log_in,
  2504. &quot;Login&quot;,
  2505. }
  2506. })
  2507. <span class="boring">}
  2508. </span></code></pre></pre>
  2509. <blockquote>
  2510. <p>Note: <code>spawn</code> will always spawn a <em>new</em> future. You most likely don't want to call it on every render.</p>
  2511. </blockquote>
  2512. <p>Calling <code>spawn</code> will give you a <code>JoinHandle</code> which lets you cancel or pause the future.</p>
  2513. <h2 id="spawning-tokio-tasks"><a class="header" href="#spawning-tokio-tasks">Spawning Tokio Tasks</a></h2>
  2514. <p>Sometimes, you might want to spawn a background task that needs multiple threads or talk to hardware that might block your app code. In these cases, we can directly spawn a Tokio task from our future. For Dioxus-Desktop, your task will be spawned onto Tokio's Multithreaded runtime:</p>
  2515. <pre><pre class="playground"><code class="language-rust edition2018">
  2516. <span class="boring">#![allow(unused)]
  2517. </span><span class="boring">fn main() {
  2518. </span> cx.spawn(async {
  2519. let _ = tokio::spawn(async {}).await;
  2520. let _ = tokio::task::spawn_local(async {
  2521. // some !Send work
  2522. })
  2523. .await;
  2524. });
  2525. <span class="boring">}
  2526. </span></code></pre></pre>
  2527. <div style="break-before: page; page-break-before: always;"></div><h1 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h1>
  2528. <h2 id="reusable-components"><a class="header" href="#reusable-components">Reusable Components</a></h2>
  2529. <p>As much as possible, break your code down into small, reusable components and hooks, instead of implementing large chunks of the UI in a single component. This will help you keep the code maintainable – it is much easier to e.g. add, remove or re-order parts of the UI if it is organized in components.</p>
  2530. <p>Organize your components in modules to keep the codebase easy to navigate!</p>
  2531. <h2 id="minimize-state-dependencies"><a class="header" href="#minimize-state-dependencies">Minimize State Dependencies</a></h2>
  2532. <p>While it is possible to share state between components, this should only be done when necessary. Any component that is associated with a particular state object needs to be re-rendered when that state changes. For this reason:</p>
  2533. <ul>
  2534. <li>Keep state local to a component if possible</li>
  2535. <li>When sharing state through props, only pass down the specific data necessary</li>
  2536. </ul>
  2537. <div style="break-before: page; page-break-before: always;"></div><h1 id="error-handling"><a class="header" href="#error-handling">Error handling</a></h1>
  2538. <p>A selling point of Rust for web development is the reliability of always knowing where errors can occur and being forced to handle them</p>
  2539. <p>However, we haven't talked about error handling at all in this guide! In this chapter, we'll cover some strategies in handling errors to ensure your app never crashes.</p>
  2540. <h2 id="the-simplest--returning-none"><a class="header" href="#the-simplest--returning-none">The simplest – returning None</a></h2>
  2541. <p>Astute observers might have noticed that <code>Element</code> is actually a type alias for <code>Option&lt;VNode&gt;</code>. You don't need to know what a <code>VNode</code> is, but it's important to recognize that we could actually return nothing at all:</p>
  2542. <pre><pre class="playground"><code class="language-rust edition2018">
  2543. <span class="boring">#![allow(unused)]
  2544. </span><span class="boring">fn main() {
  2545. </span>fn App(cx: Scope) -&gt; Element {
  2546. None
  2547. }
  2548. <span class="boring">}
  2549. </span></code></pre></pre>
  2550. <p>This lets us add in some syntactic sugar for operations we think <em>shouldn't</em> fail, but we're still not confident enough to &quot;unwrap&quot; on.</p>
  2551. <blockquote>
  2552. <p>The nature of <code>Option&lt;VNode&gt;</code> might change in the future as the <code>try</code> trait gets upgraded.</p>
  2553. </blockquote>
  2554. <pre><pre class="playground"><code class="language-rust edition2018">
  2555. <span class="boring">#![allow(unused)]
  2556. </span><span class="boring">fn main() {
  2557. </span>fn App(cx: Scope) -&gt; Element {
  2558. // immediately return &quot;None&quot;
  2559. let name = cx.use_hook(|_| Some(&quot;hi&quot;))?;
  2560. }
  2561. <span class="boring">}
  2562. </span></code></pre></pre>
  2563. <h2 id="early-return-on-result"><a class="header" href="#early-return-on-result">Early return on result</a></h2>
  2564. <p>Because Rust can't accept both Options and Results with the existing try infrastructure, you'll need to manually handle Results. This can be done by converting them into Options or by explicitly handling them.</p>
  2565. <pre><pre class="playground"><code class="language-rust edition2018">
  2566. <span class="boring">#![allow(unused)]
  2567. </span><span class="boring">fn main() {
  2568. </span>fn App(cx: Scope) -&gt; Element {
  2569. // Convert Result to Option
  2570. let name = cx.use_hook(|_| &quot;1.234&quot;).parse().ok()?;
  2571. // Early return
  2572. let count = cx.use_hook(|_| &quot;1.234&quot;);
  2573. let val = match count.parse() {
  2574. Ok(val) =&gt; val
  2575. Err(err) =&gt; return cx.render(rsx!{ &quot;Parsing failed&quot; })
  2576. };
  2577. }
  2578. <span class="boring">}
  2579. </span></code></pre></pre>
  2580. <p>Notice that while hooks in Dioxus do not like being called in conditionals or loops, they <em>are</em> okay with early returns. Returning an error state early is a completely valid way of handling errors.</p>
  2581. <h2 id="match-results"><a class="header" href="#match-results">Match results</a></h2>
  2582. <p>The next &quot;best&quot; way of handling errors in Dioxus is to match on the error locally. This is the most robust way of handling errors, though it doesn't scale to architectures beyond a single component.</p>
  2583. <p>To do this, we simply have an error state built into our component:</p>
  2584. <pre><pre class="playground"><code class="language-rust edition2018">
  2585. <span class="boring">#![allow(unused)]
  2586. </span><span class="boring">fn main() {
  2587. </span>let err = use_state(cx, || None);
  2588. <span class="boring">}
  2589. </span></code></pre></pre>
  2590. <p>Whenever we perform an action that generates an error, we'll set that error state. We can then match on the error in a number of ways (early return, return Element, etc).</p>
  2591. <pre><pre class="playground"><code class="language-rust edition2018">
  2592. <span class="boring">#![allow(unused)]
  2593. </span><span class="boring">fn main() {
  2594. </span>fn Commandline(cx: Scope) -&gt; Element {
  2595. let error = use_state(cx, || None);
  2596. cx.render(match *error {
  2597. Some(error) =&gt; rsx!(
  2598. h1 { &quot;An error occured&quot; }
  2599. )
  2600. None =&gt; rsx!(
  2601. input {
  2602. oninput: move |_| error.set(Some(&quot;bad thing happened!&quot;)),
  2603. }
  2604. )
  2605. })
  2606. }
  2607. <span class="boring">}
  2608. </span></code></pre></pre>
  2609. <h2 id="passing-error-states-through-components"><a class="header" href="#passing-error-states-through-components">Passing error states through components</a></h2>
  2610. <p>If you're dealing with a handful of components with minimal nesting, you can just pass the error handle into child components.</p>
  2611. <pre><pre class="playground"><code class="language-rust edition2018">
  2612. <span class="boring">#![allow(unused)]
  2613. </span><span class="boring">fn main() {
  2614. </span>fn Commandline(cx: Scope) -&gt; Element {
  2615. let error = use_state(cx, || None);
  2616. if let Some(error) = **error {
  2617. return cx.render(rsx!{ &quot;An error occured&quot; });
  2618. }
  2619. cx.render(rsx!{
  2620. Child { error: error.clone() }
  2621. Child { error: error.clone() }
  2622. Child { error: error.clone() }
  2623. Child { error: error.clone() }
  2624. })
  2625. }
  2626. <span class="boring">}
  2627. </span></code></pre></pre>
  2628. <p>Much like before, our child components can manually set the error during their own actions. The advantage to this pattern is that we can easily isolate error states to a few components at a time, making our app more predictable and robust.</p>
  2629. <h2 id="going-global"><a class="header" href="#going-global">Going global</a></h2>
  2630. <p>A strategy for handling cascaded errors in larger apps is through signaling an error using global state. This particular pattern involves creating an &quot;error&quot; context, and then setting it wherever relevant. This particular method is not as &quot;sophisticated&quot; as React's error boundary, but it is more fitting for Rust.</p>
  2631. <p>To get started, consider using a built-in hook like <code>use_context</code> and <code>use_context_provider</code> or Fermi. Of course, it's pretty easy to roll your own hook too.</p>
  2632. <p>At the &quot;top&quot; of our architecture, we're going to want to explicitly declare a value that could be an error.</p>
  2633. <pre><pre class="playground"><code class="language-rust edition2018">
  2634. <span class="boring">#![allow(unused)]
  2635. </span><span class="boring">fn main() {
  2636. </span>enum InputError {
  2637. None,
  2638. TooLong,
  2639. TooShort,
  2640. }
  2641. static INPUT_ERROR: Atom&lt;InputError&gt; = |_| InputError::None;
  2642. <span class="boring">}
  2643. </span></code></pre></pre>
  2644. <p>Then, in our top level component, we want to explicitly handle the possible error state for this part of the tree.</p>
  2645. <pre><pre class="playground"><code class="language-rust edition2018">
  2646. <span class="boring">#![allow(unused)]
  2647. </span><span class="boring">fn main() {
  2648. </span>fn TopLevel(cx: Scope) -&gt; Element {
  2649. let error = use_read(cx, INPUT_ERROR);
  2650. match error {
  2651. TooLong =&gt; return cx.render(rsx!{ &quot;FAILED: Too long!&quot; }),
  2652. TooShort =&gt; return cx.render(rsx!{ &quot;FAILED: Too Short!&quot; }),
  2653. _ =&gt; {}
  2654. }
  2655. }
  2656. <span class="boring">}
  2657. </span></code></pre></pre>
  2658. <p>Now, whenever a downstream component has an error in its actions, it can simply just set its own error state:</p>
  2659. <pre><pre class="playground"><code class="language-rust edition2018">
  2660. <span class="boring">#![allow(unused)]
  2661. </span><span class="boring">fn main() {
  2662. </span>fn Commandline(cx: Scope) -&gt; Element {
  2663. let set_error = use_set(cx, INPUT_ERROR);
  2664. cx.render(rsx!{
  2665. input {
  2666. oninput: move |evt| {
  2667. if evt.value.len() &gt; 20 {
  2668. set_error(InputError::TooLong);
  2669. }
  2670. }
  2671. }
  2672. })
  2673. }
  2674. <span class="boring">}
  2675. </span></code></pre></pre>
  2676. <p>This approach to error handling is best in apps that have &quot;well defined&quot; error states. Consider using a crate like <code>thiserror</code> or <code>anyhow</code> to simplify the generation of the error types.</p>
  2677. <p>This pattern is widely popular in many contexts and is particularly helpful whenever your code generates a non-recoverable error. You can gracefully capture these &quot;global&quot; error states without panicking or mucking up state.</p>
  2678. <div style="break-before: page; page-break-before: always;"></div><h1 id="antipatterns"><a class="header" href="#antipatterns">Antipatterns</a></h1>
  2679. <p>This example shows what not to do and provides a reason why a given pattern is considered an &quot;AntiPattern&quot;. Most anti-patterns are considered wrong for performance or code re-usability reasons.</p>
  2680. <h2 id="unnecessarily-nested-fragments"><a class="header" href="#unnecessarily-nested-fragments">Unnecessarily Nested Fragments</a></h2>
  2681. <p>Fragments don't mount a physical element to the DOM immediately, so Dioxus must recurse into its children to find a physical DOM node. This process is called &quot;normalization&quot;. This means that deeply nested fragments make Dioxus perform unnecessary work. Prefer one or two levels of fragments / nested components until presenting a true DOM element.</p>
  2682. <p>Only Component and Fragment nodes are susceptible to this issue. Dioxus mitigates this with components by providing an API for registering shared state without the Context Provider pattern.</p>
  2683. <pre><pre class="playground"><code class="language-rust edition2018">
  2684. <span class="boring">#![allow(unused)]
  2685. </span><span class="boring">fn main() {
  2686. </span> // ❌ Don't unnecessarily nest fragments
  2687. let _ = cx.render(rsx!(
  2688. Fragment {
  2689. Fragment {
  2690. Fragment {
  2691. Fragment {
  2692. Fragment {
  2693. div { &quot;Finally have a real node!&quot; }
  2694. }
  2695. }
  2696. }
  2697. }
  2698. }
  2699. ));
  2700. // ✅ Render shallow structures
  2701. cx.render(rsx!(
  2702. div { &quot;Finally have a real node!&quot; }
  2703. ))
  2704. <span class="boring">}
  2705. </span></code></pre></pre>
  2706. <h2 id="incorrect-iterator-keys"><a class="header" href="#incorrect-iterator-keys">Incorrect Iterator Keys</a></h2>
  2707. <p>As described in the <a href="best_practices/../interactivity/dynamic_rendering.html#the-key-attribute">dynamic rendering chapter</a>, list items must have unique keys that are associated with the same items across renders. This helps Dioxus associate state with the contained components and ensures good diffing performance. Do not omit keys, unless you know that the list will never change.</p>
  2708. <pre><pre class="playground"><code class="language-rust edition2018">
  2709. <span class="boring">#![allow(unused)]
  2710. </span><span class="boring">fn main() {
  2711. </span> let data: &amp;HashMap&lt;_, _&gt; = &amp;cx.props.data;
  2712. // ❌ No keys
  2713. cx.render(rsx! {
  2714. ul {
  2715. data.values().map(|value| rsx!(
  2716. li { &quot;List item: {value}&quot; }
  2717. ))
  2718. }
  2719. });
  2720. // ❌ Using index as keys
  2721. cx.render(rsx! {
  2722. ul {
  2723. cx.props.data.values().enumerate().map(|(index, value)| rsx!(
  2724. li { key: &quot;{index}&quot;, &quot;List item: {value}&quot; }
  2725. ))
  2726. }
  2727. });
  2728. // ✅ Using unique IDs as keys:
  2729. cx.render(rsx! {
  2730. ul {
  2731. cx.props.data.iter().map(|(key, value)| rsx!(
  2732. li { key: &quot;{key}&quot;, &quot;List item: {value}&quot; }
  2733. ))
  2734. }
  2735. })
  2736. <span class="boring">}
  2737. </span></code></pre></pre>
  2738. <h2 id="avoid-interior-mutability-in-props"><a class="header" href="#avoid-interior-mutability-in-props">Avoid Interior Mutability in Props</a></h2>
  2739. <p>While it is technically acceptable to have a <code>Mutex</code> or a <code>RwLock</code> in the props, they will be difficult to use.</p>
  2740. <p>Suppose you have a struct <code>User</code> containing the field <code>username: String</code>. If you pass a <code>Mutex&lt;User&gt;</code> prop to a <code>UserComponent</code> component, that component may wish to pass the username as a <code>&amp;str</code> prop to a child component. However, it cannot pass that borrowed field down, since it only would live as long as the <code>Mutex</code>'s lock, which belongs to the <code>UserComponent</code> function. Therefore, the component will be forced to clone the <code>username</code> field.</p>
  2741. <h2 id="avoid-updating-state-during-render"><a class="header" href="#avoid-updating-state-during-render">Avoid Updating State During Render</a></h2>
  2742. <p>Every time you update the state, Dioxus needs to re-render the component – this is inefficient! Consider refactoring your code to avoid this.</p>
  2743. <p>Also, if you unconditionally update the state during render, it will be re-rendered in an infinite loop.</p>
  2744. <div style="break-before: page; page-break-before: always;"></div><h1 id="publishing"><a class="header" href="#publishing">Publishing</a></h1>
  2745. <div style="break-before: page; page-break-before: always;"></div><h1 id="publishing-1"><a class="header" href="#publishing-1">Publishing</a></h1>
  2746. <p>Congrats! You've made your first Dioxus app that actually does some pretty cool stuff. This app uses your operating system's WebView library, so it's portable to be distributed for other platforms.</p>
  2747. <p>In this section, we'll cover how to bundle your app for macOS, Windows, and Linux.</p>
  2748. <h2 id="install-cargo-bundle"><a class="header" href="#install-cargo-bundle">Install <code>cargo-bundle</code></a></h2>
  2749. <p>The first thing we'll do is install <a href="https://github.com/burtonageo/cargo-bundle"><code>cargo-bundle</code></a>. This extension to cargo will make it very easy to package our app for the various platforms.</p>
  2750. <p>According to the <code>cargo-bundle</code> github page, </p>
  2751. <p><em>&quot;cargo-bundle is a tool used to generate installers or app bundles for GUI executables built with cargo. It can create .app bundles for Mac OS X and iOS, .deb packages for Linux, and .msi installers for Windows (note however that iOS and Windows support is still experimental). Support for creating .rpm packages (for Linux) and .apk packages (for Android) is still pending.&quot;</em></p>
  2752. <p>To install, simply run</p>
  2753. <p><code>cargo install cargo-bundle</code></p>
  2754. <h2 id="setting-up-your-project"><a class="header" href="#setting-up-your-project">Setting up your project</a></h2>
  2755. <p>To get a project setup for bundling, we need to add some flags to our <code>Cargo.toml</code> file. </p>
  2756. <pre><code class="language-toml">[package]
  2757. name = &quot;example&quot;
  2758. # ...other fields...
  2759. [package.metadata.bundle]
  2760. name = &quot;DogSearch&quot;
  2761. identifier = &quot;com.dogs.dogsearch&quot;
  2762. version = &quot;1.0.0&quot;
  2763. copyright = &quot;Copyright (c) Jane Doe 2016. All rights reserved.&quot;
  2764. category = &quot;Developer Tool&quot;
  2765. short_description = &quot;Easily search for Dog photos&quot;
  2766. long_description = &quot;&quot;&quot;
  2767. This app makes it quick and easy to browse photos of dogs from over 200 bree
  2768. &quot;&quot;&quot;
  2769. </code></pre>
  2770. <h2 id="building"><a class="header" href="#building">Building</a></h2>
  2771. <p>Following cargo-bundle's instructions, we simply <code>cargo-bundle --release</code> to produce a final app with all the optimizations and assets builtin.</p>
  2772. <p>Once you've ran <code>cargo-bundle --release</code>, your app should be accessible in</p>
  2773. <p><code>target/release/bundle/&lt;platform&gt;/</code>.</p>
  2774. <p>For example, a macOS app would look like this:</p>
  2775. <p><img src="publishing/../images/publish.png" alt="Published App" /></p>
  2776. <p>Nice! And it's only 4.8 Mb – extremely lean!! Because Dioxus leverages your platform's native WebView, Dioxus apps are extremely memory efficient and won't waste your battery.</p>
  2777. <blockquote>
  2778. <p>Note: not all CSS works the same on all platforms. Make sure to view your app's CSS on each platform – or web browser (Firefox, Chrome, Safari) before publishing.</p>
  2779. </blockquote>
  2780. <div style="break-before: page; page-break-before: always;"></div><h2 id="publishing-with-github-pages"><a class="header" href="#publishing-with-github-pages">Publishing with Github Pages</a></h2>
  2781. <p>To build our app and publish it to Github:</p>
  2782. <ul>
  2783. <li>Make sure GitHub Pages is set up for your repo</li>
  2784. <li>Build your app with <code>trunk build --release</code> (include <code>--public-url &lt;repo-name&gt;</code> to update asset prefixes if using a project site)</li>
  2785. <li>Move your generated HTML/CSS/JS/Wasm from <code>dist</code> into the folder configured for Github Pages</li>
  2786. <li>Add and commit with git</li>
  2787. <li>Push to GitHub</li>
  2788. </ul>
  2789. <div style="break-before: page; page-break-before: always;"></div><h1 id="fullstack-development"><a class="header" href="#fullstack-development">Fullstack development</a></h1>
  2790. <p>So far you have learned about three different approaches to target the web with Dioxus:</p>
  2791. <ul>
  2792. <li><a href="fullstack/../getting_started/web.html">Client-side rendering with dioxus-web</a></li>
  2793. <li><a href="fullstack/../getting_started/liveview.html">Server-side rendering with dioxus-liveview</a></li>
  2794. <li><a href="fullstack/../getting_started/ssr.html">Server-side static HTML generation with dioxus-ssr</a></li>
  2795. </ul>
  2796. <h2 id="summary-of-existing-approaches"><a class="header" href="#summary-of-existing-approaches">Summary of Existing Approaches</a></h2>
  2797. <p>Each approach has its tradeoffs:</p>
  2798. <h3 id="client-side-rendering"><a class="header" href="#client-side-rendering">Client-side rendering</a></h3>
  2799. <ul>
  2800. <li>
  2801. <p>With Client side rendering, you send the entire content of your application to the client, and then the client generates all of the HTML of the page dynamically.</p>
  2802. </li>
  2803. <li>
  2804. <p>This means that the page will be blank until the JavaScript bundle has loaded and the application has initialized. This can result in <strong>slower first render times and makes the page less SEO-friendly</strong>.</p>
  2805. </li>
  2806. </ul>
  2807. <blockquote>
  2808. <p>SEO stands for Search Engine Optimization. It refers to the practice of making your website more likely to appear in search engine results. Search engines like Google and Bing use web crawlers to index the content of websites. Most of these crawlers are not able to run JavaScript, so they will not be able to index the content of your page if it is rendered client-side.</p>
  2809. </blockquote>
  2810. <ul>
  2811. <li>Client-side rendered applications need to use <strong>weakly typed requests to communicate with the server</strong></li>
  2812. </ul>
  2813. <blockquote>
  2814. <p>Client-side rendering is a good starting point for most applications. It is well supported and makes it easy to communicate with the client/browser APIs</p>
  2815. </blockquote>
  2816. <h3 id="liveview-1"><a class="header" href="#liveview-1">Liveview</a></h3>
  2817. <ul>
  2818. <li>
  2819. <p>Liveview rendering communicates with the server over a WebSocket connection. It essentially moves all of the work that Client-side rendering does to the server.</p>
  2820. </li>
  2821. <li>
  2822. <p>This makes it <strong>easy to communicate with the server, but more difficult to communicate with the client/browser APIS</strong>.</p>
  2823. </li>
  2824. <li>
  2825. <p>Each interaction also requires a message to be sent to the server and back which can cause <strong>issues with latency</strong>.</p>
  2826. </li>
  2827. <li>
  2828. <p>Because Liveview uses a websocket to render, the page will be blank until the WebSocket connection has been established and the first renderer has been sent form the websocket. Just like with client side rendering, this can make the page <strong>less SEO-friendly</strong>.</p>
  2829. </li>
  2830. <li>
  2831. <p>Because the page is rendered on the server and the page is sent to the client piece by piece, you never need to send the entire application to the client. The initial load time can be faster than client-side rendering with large applications because Liveview only needs to send a constant small websocket script regardless of the size of the application.</p>
  2832. </li>
  2833. </ul>
  2834. <blockquote>
  2835. <p>Liveview is a good fit for applications that already need to communicate with the server frequently (like real time collaborative apps), but don't need to communicate with as many client/browser APIs</p>
  2836. </blockquote>
  2837. <h3 id="server-side-rendering-1"><a class="header" href="#server-side-rendering-1">Server-side rendering</a></h3>
  2838. <ul>
  2839. <li>Server-side rendering generates all of the HTML of the page on the server before the page is sent to the client. This means that the page will be fully rendered when it is sent to the client. This results in a faster first render time and makes the page more SEO-friendly. However, it <strong>only works for static pages</strong>.</li>
  2840. </ul>
  2841. <blockquote>
  2842. <p>Server-side rendering is not a good fit for purely static sites like a blog</p>
  2843. </blockquote>
  2844. <h2 id="a-new-approach"><a class="header" href="#a-new-approach">A New Approach</a></h2>
  2845. <p>Each of these approaches has its tradeoffs. What if we could combine the best parts of each approach?</p>
  2846. <ul>
  2847. <li><strong>Fast initial render</strong> time like SSR</li>
  2848. <li><strong>Works well with SEO</strong> like SSR</li>
  2849. <li><strong>Type safe easy communication with the server</strong> like Liveview</li>
  2850. <li><strong>Access to the client/browser APIs</strong> like Client-side rendering</li>
  2851. <li><strong>Fast interactivity</strong> like Client-side rendering</li>
  2852. </ul>
  2853. <p>We can achieve this by rendering the initial page on the server (SSR) and then taking over rendering on the client (Client-side rendering). Taking over rendering on the client is called <strong>hydration</strong>.</p>
  2854. <p>Finally, we can use <a href="fullstack/server_functions.html">server functions</a> to communicate with the server in a type-safe way.</p>
  2855. <p>This approach uses both the dioxus-web and dioxus-ssr crates. To integrate those two packages and <code>axum</code>, <code>warp</code>, or <code>salvo</code>, Dioxus provides the <code>dioxus-fullstack</code> crate.</p>
  2856. <div style="break-before: page; page-break-before: always;"></div><blockquote>
  2857. <p>This guide assumes you read the <a href="fullstack/web.html">Web</a> guide and installed the <a href="https://github.com/DioxusLabs/cli">Dioxus-cli</a></p>
  2858. </blockquote>
  2859. <h1 id="getting-started-2"><a class="header" href="#getting-started-2">Getting Started</a></h1>
  2860. <h2 id="setup-4"><a class="header" href="#setup-4">Setup</a></h2>
  2861. <p>For this guide, we're going to show how to use Dioxus with <a href="https://docs.rs/axum/latest/axum/">Axum</a>, but <code>dioxus-fullstack</code> also integrates with the <a href="https://docs.rs/warp/latest/warp/">Warp</a> and <a href="https://docs.rs/salvo/latest/salvo/">Salvo</a> web frameworks.</p>
  2862. <p>Make sure you have Rust and Cargo installed, and then create a new project:</p>
  2863. <pre><code class="language-shell">cargo new --bin demo
  2864. cd demo
  2865. </code></pre>
  2866. <p>Add <code>dioxus</code> and <code>dioxus-fullstack</code> as dependencies:</p>
  2867. <pre><code class="language-shell">cargo add dioxus
  2868. cargo add dioxus-fullstack --features axum, ssr
  2869. </code></pre>
  2870. <p>Next, add all the Axum dependencies. This will be different if you're using a different Web Framework</p>
  2871. <pre><code class="language-shell">cargo add tokio --features full
  2872. cargo add axum
  2873. </code></pre>
  2874. <p>Your dependencies should look roughly like this:</p>
  2875. <pre><code class="language-toml">[dependencies]
  2876. axum = &quot;*&quot;
  2877. dioxus = { version = &quot;*&quot; }
  2878. dioxus-fullstack = { version = &quot;*&quot;, features = [&quot;axum&quot;, &quot;ssr&quot;] }
  2879. tokio = { version = &quot;*&quot;, features = [&quot;full&quot;] }
  2880. </code></pre>
  2881. <p>Now, set up your Axum app to serve the Dioxus app.</p>
  2882. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case, unused)]
  2883. use dioxus::prelude::*;
  2884. #[tokio::main]
  2885. async fn main() {
  2886. #[cfg(feature = &quot;ssr&quot;)]
  2887. {
  2888. use dioxus_fullstack::prelude::*;
  2889. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  2890. axum::Server::bind(&amp;addr)
  2891. .serve(
  2892. axum::Router::new()
  2893. .serve_dioxus_application(&quot;&quot;, ServeConfigBuilder::new(app, ()))
  2894. .into_make_service(),
  2895. )
  2896. .await
  2897. .unwrap();
  2898. }
  2899. }
  2900. fn app(cx: Scope) -&gt; Element {
  2901. let mut count = use_state(cx, || 0);
  2902. cx.render(rsx! {
  2903. h1 { &quot;High-Five counter: {count}&quot; }
  2904. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  2905. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  2906. })
  2907. }
  2908. </code></pre></pre>
  2909. <p>Now, run your app with <code>cargo run</code> and open <code>http://localhost:8080</code> in your browser. You should see a server-side rendered page with a counter.</p>
  2910. <h2 id="hydration"><a class="header" href="#hydration">Hydration</a></h2>
  2911. <p>Right now, the page is static. We can't interact with the buttons. To fix this, we can hydrate the page with <code>dioxus-web</code>.</p>
  2912. <p>First, modify your <code>Cargo.toml</code> to include two features, one for the server called <code>ssr</code>, and one for the client called <code>web</code>.</p>
  2913. <pre><code class="language-toml">[dependencies]
  2914. # Common dependancies
  2915. dioxus = { version = &quot;*&quot; }
  2916. dioxus-fullstack = { version = &quot;*&quot; }
  2917. # Web dependancies
  2918. dioxus-web = { version = &quot;*&quot;, features=[&quot;hydrate&quot;], optional = true }
  2919. # Server dependancies
  2920. axum = { version = &quot;0.6.12&quot;, optional = true }
  2921. tokio = { version = &quot;1.27.0&quot;, features = [&quot;full&quot;], optional = true }
  2922. [features]
  2923. default = []
  2924. ssr = [&quot;axum&quot;, &quot;tokio&quot;, &quot;dioxus-fullstack/axum&quot;]
  2925. web = [&quot;dioxus-web&quot;]
  2926. </code></pre>
  2927. <p>Next, we need to modify our <code>main.rs</code> to use either hydrate on the client or render on the server depending on the active features.</p>
  2928. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case, unused)]
  2929. use dioxus::prelude::*;
  2930. fn main() {
  2931. #[cfg(feature = &quot;web&quot;)]
  2932. dioxus_web::launch_cfg(app, dioxus_web::Config::new().hydrate(true));
  2933. #[cfg(feature = &quot;ssr&quot;)]
  2934. {
  2935. use dioxus_fullstack::prelude::*;
  2936. tokio::runtime::Runtime::new()
  2937. .unwrap()
  2938. .block_on(async move {
  2939. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  2940. axum::Server::bind(&amp;addr)
  2941. .serve(
  2942. axum::Router::new()
  2943. .serve_dioxus_application(&quot;&quot;, ServeConfigBuilder::new(app, ()))
  2944. .into_make_service(),
  2945. )
  2946. .await
  2947. .unwrap();
  2948. });
  2949. }
  2950. }
  2951. fn app(cx: Scope) -&gt; Element {
  2952. let mut count = use_state(cx, || 0);
  2953. cx.render(rsx! {
  2954. h1 { &quot;High-Five counter: {count}&quot; }
  2955. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  2956. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  2957. })
  2958. }
  2959. </code></pre></pre>
  2960. <p>Now, build your client-side bundle with <code>dioxus build --features web</code> and run your server with <code>cargo run --features ssr</code>. You should see the same page as before, but now you can interact with the buttons!</p>
  2961. <h2 id="sycronizing-props-between-the-server-and-client"><a class="header" href="#sycronizing-props-between-the-server-and-client">Sycronizing props between the server and client</a></h2>
  2962. <p>Let's make the initial count of the counter dynamic based on the current page.</p>
  2963. <h3 id="modifying-the-server"><a class="header" href="#modifying-the-server">Modifying the server</a></h3>
  2964. <p>To do this, we must remove the serve_dioxus_application and replace it with a custom implementation of its four key functions:</p>
  2965. <ul>
  2966. <li>Serve static WASM and JS files with serve_static_assets</li>
  2967. <li>Register server functions with register_server_fns (more information on server functions later)</li>
  2968. <li>Connect to the hot reload server with connect_hot_reload</li>
  2969. <li>A custom route that uses SSRState to server-side render the application</li>
  2970. </ul>
  2971. <h3 id="modifying-the-client"><a class="header" href="#modifying-the-client">Modifying the client</a></h3>
  2972. <p>The only thing we need to change on the client is the props. <code>dioxus-fullstack</code> will automatically serialize the props it uses to server render the app and send them to the client. In the client section of <code>main.rs</code>, we need to add <code>get_root_props_from_document</code> to deserialize the props before we hydrate the app.</p>
  2973. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case, unused)]
  2974. use dioxus::prelude::*;
  2975. use dioxus_fullstack::prelude::*;
  2976. fn main() {
  2977. #[cfg(feature = &quot;web&quot;)]
  2978. dioxus_web::launch_with_props(
  2979. app,
  2980. // Get the root props from the document
  2981. get_root_props_from_document().unwrap_or_default(),
  2982. dioxus_web::Config::new().hydrate(true),
  2983. );
  2984. #[cfg(feature = &quot;ssr&quot;)]
  2985. {
  2986. use axum::extract::Path;
  2987. use axum::extract::State;
  2988. use axum::routing::get;
  2989. tokio::runtime::Runtime::new()
  2990. .unwrap()
  2991. .block_on(async move {
  2992. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  2993. axum::Server::bind(&amp;addr)
  2994. .serve(
  2995. axum::Router::new()
  2996. // Serve the dist folder with the static javascript and WASM files created by the dixous CLI
  2997. .serve_static_assets(&quot;./dist&quot;)
  2998. // Register server functions
  2999. .register_server_fns(&quot;&quot;)
  3000. // Connect to the hot reload server in debug mode
  3001. .connect_hot_reload()
  3002. // Render the application. This will serialize the root props (the intial count) into the HTML
  3003. .route(
  3004. &quot;/&quot;,
  3005. get(move | State(ssr_state): State&lt;SSRState&gt;| async move { axum::body::Full::from(
  3006. ssr_state.render(
  3007. &amp;ServeConfigBuilder::new(
  3008. app,
  3009. 0,
  3010. )
  3011. .build(),
  3012. )
  3013. )}),
  3014. )
  3015. // Render the application with a different intial count
  3016. .route(
  3017. &quot;/:initial_count&quot;,
  3018. get(move |Path(intial_count): Path&lt;usize&gt;, State(ssr_state): State&lt;SSRState&gt;| async move { axum::body::Full::from(
  3019. ssr_state.render(
  3020. &amp;ServeConfigBuilder::new(
  3021. app,
  3022. intial_count,
  3023. )
  3024. .build(),
  3025. )
  3026. )}),
  3027. )
  3028. .with_state(SSRState::default())
  3029. .into_make_service(),
  3030. )
  3031. .await
  3032. .unwrap();
  3033. });
  3034. }
  3035. }
  3036. fn app(cx: Scope&lt;usize&gt;) -&gt; Element {
  3037. let mut count = use_state(cx, || *cx.props);
  3038. cx.render(rsx! {
  3039. h1 { &quot;High-Five counter: {count}&quot; }
  3040. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  3041. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  3042. })
  3043. }
  3044. </code></pre></pre>
  3045. <p>Now, build your client-side bundle with <code>dioxus build --features web</code> and run your server with <code>cargo run --features ssr</code>. Navigate to <code>http://localhost:8080/1</code> and you should see the counter start at 1. Navigate to <code>http://localhost:8080/2</code> and you should see the counter start at 2.</p>
  3046. <div style="break-before: page; page-break-before: always;"></div><h1 id="communicating-with-the-server"><a class="header" href="#communicating-with-the-server">Communicating with the server</a></h1>
  3047. <p><code>dixous-server</code> provides server functions that allow you to call an automatically generated API on the server from the client as if it were a local function.</p>
  3048. <p>To make a server function, simply add the <code>#[server(YourUniqueType)]</code> attribute to a function. The function must:</p>
  3049. <ul>
  3050. <li>Be an async function</li>
  3051. <li>Have arguments and a return type that both implement serialize and deserialize (with <a href="https://serde.rs/">serde</a>).</li>
  3052. <li>Return a <code>Result</code> with an error type of ServerFnError</li>
  3053. </ul>
  3054. <p>You must call <code>register</code> on the type you passed into the server macro in your main function before starting your server to tell Dioxus about the server function.</p>
  3055. <p>Let's continue building on the app we made in the <a href="fullstack/./getting_started.html">getting started</a> guide. We will add a server function to our app that allows us to double the count on the server.</p>
  3056. <p>First, add serde as a dependency:</p>
  3057. <pre><code class="language-shell">cargo add serde
  3058. </code></pre>
  3059. <p>Next, add the server function to your <code>main.rs</code>:</p>
  3060. <pre><pre class="playground"><code class="language-rust edition2018">#![allow(non_snake_case, unused)]
  3061. use dioxus::prelude::*;
  3062. use dioxus_fullstack::prelude::*;
  3063. fn main() {
  3064. #[cfg(feature = &quot;web&quot;)]
  3065. dioxus_web::launch_with_props(
  3066. app,
  3067. // Get the root props from the document
  3068. get_root_props_from_document().unwrap_or_default(),
  3069. dioxus_web::Config::new().hydrate(true),
  3070. );
  3071. #[cfg(feature = &quot;ssr&quot;)]
  3072. {
  3073. use axum::extract::Path;
  3074. use axum::extract::State;
  3075. use axum::routing::get;
  3076. // Register the server function before starting the server
  3077. DoubleServer::register().unwrap();
  3078. tokio::runtime::Runtime::new()
  3079. .unwrap()
  3080. .block_on(async move {
  3081. let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080));
  3082. axum::Server::bind(&amp;addr)
  3083. .serve(
  3084. axum::Router::new()
  3085. // Serve the dist folder with the static javascript and WASM files created by the dixous CLI
  3086. .serve_static_assets(&quot;./dist&quot;)
  3087. // Register server functions
  3088. .register_server_fns(&quot;&quot;)
  3089. // Connect to the hot reload server in debug mode
  3090. .connect_hot_reload()
  3091. // Render the application. This will serialize the root props (the intial count) into the HTML
  3092. .route(
  3093. &quot;/&quot;,
  3094. get(move |Path(intial_count): Path&lt;usize&gt;, State(ssr_state): State&lt;SSRState&gt;| async move { axum::body::Full::from(
  3095. ssr_state.render(
  3096. &amp;ServeConfigBuilder::new(
  3097. app,
  3098. intial_count,
  3099. )
  3100. .build(),
  3101. )
  3102. )}),
  3103. )
  3104. // Render the application with a different intial count
  3105. .route(
  3106. &quot;/:initial_count&quot;,
  3107. get(move |Path(intial_count): Path&lt;usize&gt;, State(ssr_state): State&lt;SSRState&gt;| async move { axum::body::Full::from(
  3108. ssr_state.render(
  3109. &amp;ServeConfigBuilder::new(
  3110. app,
  3111. intial_count,
  3112. )
  3113. .build(),
  3114. )
  3115. )}),
  3116. )
  3117. .with_state(SSRState::default())
  3118. .into_make_service(),
  3119. )
  3120. .await
  3121. .unwrap();
  3122. });
  3123. }
  3124. }
  3125. fn app(cx: Scope&lt;usize&gt;) -&gt; Element {
  3126. let mut count = use_state(cx, || *cx.props);
  3127. cx.render(rsx! {
  3128. h1 { &quot;High-Five counter: {count}&quot; }
  3129. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  3130. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  3131. button {
  3132. onclick: move |_| {
  3133. to_owned![count];
  3134. async move {
  3135. // Call the server function just like a local async function
  3136. if let Ok(new_count) = double_server(*count.current()).await {
  3137. count.set(new_count);
  3138. }
  3139. }
  3140. },
  3141. &quot;Double&quot;
  3142. }
  3143. })
  3144. }
  3145. #[server(DoubleServer)]
  3146. async fn double_server(number: usize) -&gt; Result&lt;usize, ServerFnError&gt; {
  3147. // Perform some expensive computation or access a database on the server
  3148. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  3149. let result = number * 2;
  3150. println!(&quot;server calculated {result}&quot;);
  3151. Ok(result)
  3152. }
  3153. </code></pre></pre>
  3154. <p>Now, build your client-side bundle with <code>dioxus build --features web</code> and run your server with <code>cargo run --features ssr</code>. You should see a new button that multiplies the count by 2.</p>
  3155. <h2 id="conclusion"><a class="header" href="#conclusion">Conclusion</a></h2>
  3156. <p>That's it! You've created a full-stack Dioxus app. You can find more examples of full-stack apps and information about how to integrate with other frameworks and desktop renderers in the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/server/examples">dioxus-fullstack examples directory</a>.</p>
  3157. <div style="break-before: page; page-break-before: always;"></div><h1 id="custom-renderer"><a class="header" href="#custom-renderer">Custom Renderer</a></h1>
  3158. <p>Dioxus is an incredibly portable framework for UI development. The lessons, knowledge, hooks, and components you acquire over time can always be used for future projects. However, sometimes those projects cannot leverage a supported renderer or you need to implement your own better renderer.</p>
  3159. <p>Great news: the design of the renderer is entirely up to you! We provide suggestions and inspiration with the 1st party renderers, but only really require processing <code>Mutations</code> and sending <code>UserEvents</code>.</p>
  3160. <h2 id="the-specifics"><a class="header" href="#the-specifics">The specifics:</a></h2>
  3161. <p>Implementing the renderer is fairly straightforward. The renderer needs to:</p>
  3162. <ol>
  3163. <li>Handle the stream of edits generated by updates to the virtual DOM</li>
  3164. <li>Register listeners and pass events into the virtual DOM's event system</li>
  3165. </ol>
  3166. <p>Essentially, your renderer needs to process edits and generate events to update the VirtualDOM. From there, you'll have everything needed to render the VirtualDOM to the screen.</p>
  3167. <p>Internally, Dioxus handles the tree relationship, diffing, memory management, and the event system, leaving as little as possible required for renderers to implement themselves.</p>
  3168. <p>For reference, check out the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/interpreter">javascript interpreter</a> or <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/dioxus-tui">tui renderer</a> as a starting point for your custom renderer.</p>
  3169. <h2 id="templates"><a class="header" href="#templates">Templates</a></h2>
  3170. <p>Dioxus is built around the concept of <a href="https://docs.rs/dioxus-core/latest/dioxus_core/prelude/struct.Template.html">Templates</a>. Templates describe a UI tree known at compile time with dynamic parts filled at runtime. This is useful internally to make skip diffing static nodes, but it is also useful for the renderer to reuse parts of the UI tree. This can be useful for things like a list of items. Each item could contain some static parts and some dynamic parts. The renderer can use the template to create a static part of the UI once, clone it for each element in the list, and then fill in the dynamic parts.</p>
  3171. <h2 id="mutations"><a class="header" href="#mutations">Mutations</a></h2>
  3172. <p>The <code>Mutation</code> type is a serialized enum that represents an operation that should be applied to update the UI. The variants roughly follow this set:</p>
  3173. <pre><pre class="playground"><code class="language-rust edition2018">
  3174. <span class="boring">#![allow(unused)]
  3175. </span><span class="boring">fn main() {
  3176. </span>enum Mutation {
  3177. AppendChildren,
  3178. AssignId,
  3179. CreatePlaceholder,
  3180. CreateTextNode,
  3181. HydrateText,
  3182. LoadTemplate,
  3183. ReplaceWith,
  3184. ReplacePlaceholder,
  3185. InsertAfter,
  3186. InsertBefore,
  3187. SetAttribute,
  3188. SetText,
  3189. NewEventListener,
  3190. RemoveEventListener,
  3191. Remove,
  3192. PushRoot,
  3193. }
  3194. <span class="boring">}
  3195. </span></code></pre></pre>
  3196. <p>The Dioxus diffing mechanism operates as a <a href="https://en.wikipedia.org/wiki/Stack_machine">stack machine</a> where the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.LoadTemplate">LoadTemplate</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.CreatePlaceholder">CreatePlaceholder</a>, and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.CreateTextNode">CreateTextNode</a> mutations pushes a new &quot;real&quot; DOM node onto the stack and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.AppendChildren">AppendChildren</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.InsertAfter">InsertAfter</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.InsertBefore">InsertBefore</a>, <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.ReplacePlaceholder">ReplacePlaceholder</a>, and <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.ReplaceWith">ReplaceWith</a> all remove nodes from the stack.</p>
  3197. <h2 id="node-storage"><a class="header" href="#node-storage">Node storage</a></h2>
  3198. <p>Dioxus saves and loads elements with IDs. Inside the VirtualDOM, this is just tracked as as a u64.</p>
  3199. <p>Whenever a <code>CreateElement</code> edit is generated during diffing, Dioxus increments its node counter and assigns that new element its current NodeCount. The RealDom is responsible for remembering this ID and pushing the correct node when id is used in a mutation. Dioxus reclaims the IDs of elements when removed. To stay in sync with Dioxus you can use a sparse Vec (Vec&lt;Option<T>&gt;) with possibly unoccupied items. You can use the ids as indexes into the Vec for elements, and grow the Vec when an id does not exist.</p>
  3200. <h3 id="an-example"><a class="header" href="#an-example">An Example</a></h3>
  3201. <p>For the sake of understanding, let's consider this example – a very simple UI declaration:</p>
  3202. <pre><pre class="playground"><code class="language-rust edition2018">
  3203. <span class="boring">#![allow(unused)]
  3204. </span><span class="boring">fn main() {
  3205. </span>rsx!( h1 {&quot;count: {x}&quot;} )
  3206. <span class="boring">}
  3207. </span></code></pre></pre>
  3208. <h4 id="building-templates"><a class="header" href="#building-templates">Building Templates</a></h4>
  3209. <p>The above rsx will create a template that contains one static h1 tag and a placeholder for a dynamic text node. The template contains the static parts of the UI, and ids for the dynamic parts along with the paths to access them.</p>
  3210. <p>The template will look something like this:</p>
  3211. <pre><pre class="playground"><code class="language-rust edition2018">
  3212. <span class="boring">#![allow(unused)]
  3213. </span><span class="boring">fn main() {
  3214. </span>Template {
  3215. // Some id that is unique for the entire project
  3216. name: &quot;main.rs:1:1:0&quot;,
  3217. // The root nodes of the template
  3218. roots: &amp;[
  3219. TemplateNode::Element {
  3220. tag: &quot;h1&quot;,
  3221. namespace: None,
  3222. attrs: &amp;[],
  3223. children: &amp;[
  3224. TemplateNode::DynamicText {
  3225. id: 0
  3226. },
  3227. ],
  3228. }
  3229. ],
  3230. // the path to each of the dynamic nodes
  3231. node_paths: &amp;[
  3232. // the path to dynamic node with a id of 0
  3233. &amp;[
  3234. // on the first root node
  3235. 0,
  3236. // the first child of the root node
  3237. 0,
  3238. ]
  3239. ],
  3240. // the path to each of the dynamic attributes
  3241. attr_paths: &amp;'a [&amp;'a [u8]],
  3242. }
  3243. <span class="boring">}
  3244. </span></code></pre></pre>
  3245. <blockquote>
  3246. <p>For more detailed docs about the struture of templates see the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/prelude/struct.Template.html">Template api docs</a></p>
  3247. </blockquote>
  3248. <p>This template will be sent to the renderer in the <a href="https://docs.rs/dioxus-core/latest/dioxus_core/struct.Mutations.html#structfield.templates">list of templates</a> supplied with the mutations the first time it is used. Any time the renderer encounters a <a href="https://docs.rs/dioxus-core/latest/dioxus_core/enum.Mutation.html#variant.LoadTemplate">LoadTemplate</a> mutation after this, it should clone the template and store it in the given id.</p>
  3249. <p>For dynamic nodes and dynamic text nodes, a placeholder node should be created and inserted into the UI so that the node can be modified later.</p>
  3250. <p>In HTML renderers, this template could look like this:</p>
  3251. <pre><code class="language-html">&lt;h1&gt;&quot;&quot;&lt;/h1&gt;
  3252. </code></pre>
  3253. <h4 id="applying-mutations"><a class="header" href="#applying-mutations">Applying Mutations</a></h4>
  3254. <p>After the renderer has created all of the new templates, it can begin to process the mutations.</p>
  3255. <p>When the renderer starts, it should contain the Root node on the stack and store the Root node with an id of 0. The Root node is the top-level node of the UI. In HTML, this is the <code>&lt;div id=&quot;main&quot;&gt;</code> element.</p>
  3256. <pre><pre class="playground"><code class="language-rust edition2018">
  3257. <span class="boring">#![allow(unused)]
  3258. </span><span class="boring">fn main() {
  3259. </span>instructions: []
  3260. stack: [
  3261. RootNode,
  3262. ]
  3263. nodes: [
  3264. RootNode,
  3265. ]
  3266. <span class="boring">}
  3267. </span></code></pre></pre>
  3268. <p>The first mutation is a <code>LoadTemplate</code> mutation. This tells the renderer to load a root from the template with the given id. The renderer will then push the root node of the template onto the stack and store it with an id for later. In this case, the root node is an h1 element.</p>
  3269. <pre><pre class="playground"><code class="language-rust edition2018">
  3270. <span class="boring">#![allow(unused)]
  3271. </span><span class="boring">fn main() {
  3272. </span>instructions: [
  3273. LoadTemplate {
  3274. // the id of the template
  3275. name: &quot;main.rs:1:1:0&quot;,
  3276. // the index of the root node in the template
  3277. index: 0,
  3278. // the id to store
  3279. id: ElementId(1),
  3280. }
  3281. ]
  3282. stack: [
  3283. RootNode,
  3284. &lt;h1&gt;&quot;&quot;&lt;/h1&gt;,
  3285. ]
  3286. nodes: [
  3287. RootNode,
  3288. &lt;h1&gt;&quot;&quot;&lt;/h1&gt;,
  3289. ]
  3290. <span class="boring">}
  3291. </span></code></pre></pre>
  3292. <p>Next, Dioxus will create the dynamic text node. The diff algorithm decides that this node needs to be created, so Dioxus will generate the Mutation <code>HydrateText</code>. When the renderer receives this instruction, it will navigate to the placeholder text node in the template and replace it with the new text.</p>
  3293. <pre><pre class="playground"><code class="language-rust edition2018">
  3294. <span class="boring">#![allow(unused)]
  3295. </span><span class="boring">fn main() {
  3296. </span>instructions: [
  3297. LoadTemplate {
  3298. name: &quot;main.rs:1:1:0&quot;,
  3299. index: 0,
  3300. id: ElementId(1),
  3301. },
  3302. HydrateText {
  3303. // the id to store the text node
  3304. id: ElementId(2),
  3305. // the text to set
  3306. text: &quot;count: 0&quot;,
  3307. }
  3308. ]
  3309. stack: [
  3310. RootNode,
  3311. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  3312. ]
  3313. nodes: [
  3314. RootNode,
  3315. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  3316. &quot;count: 0&quot;,
  3317. ]
  3318. <span class="boring">}
  3319. </span></code></pre></pre>
  3320. <p>Remember, the h1 node is not attached to anything (it is unmounted) so Dioxus needs to generate an Edit that connects the h1 node to the Root. It depends on the situation, but in this case, we use <code>AppendChildren</code>. This pops the text node off the stack, leaving the Root element as the next element on the stack.</p>
  3321. <pre><pre class="playground"><code class="language-rust edition2018">
  3322. <span class="boring">#![allow(unused)]
  3323. </span><span class="boring">fn main() {
  3324. </span>instructions: [
  3325. LoadTemplate {
  3326. name: &quot;main.rs:1:1:0&quot;,
  3327. index: 0,
  3328. id: ElementId(1),
  3329. },
  3330. HydrateText {
  3331. id: ElementId(2),
  3332. text: &quot;count: 0&quot;,
  3333. },
  3334. AppendChildren {
  3335. // the id of the parent node
  3336. id: ElementId(0),
  3337. // the number of nodes to pop off the stack and append
  3338. m: 1
  3339. }
  3340. ]
  3341. stack: [
  3342. RootNode,
  3343. ]
  3344. nodes: [
  3345. RootNode,
  3346. &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;,
  3347. &quot;count: 0&quot;,
  3348. ]
  3349. <span class="boring">}
  3350. </span></code></pre></pre>
  3351. <p>Over time, our stack looked like this:</p>
  3352. <pre><pre class="playground"><code class="language-rust edition2018">
  3353. <span class="boring">#![allow(unused)]
  3354. </span><span class="boring">fn main() {
  3355. </span>[Root]
  3356. [Root, &lt;h1&gt;&quot;&quot;&lt;/h1&gt;]
  3357. [Root, &lt;h1&gt;&quot;count: 0&quot;&lt;/h1&gt;]
  3358. [Root]
  3359. <span class="boring">}
  3360. </span></code></pre></pre>
  3361. <p>Conveniently, this approach completely separates the Virtual DOM and the Real DOM. Additionally, these edits are serializable, meaning we can even manage UIs across a network connection. This little stack machine and serialized edits make Dioxus independent of platform specifics.</p>
  3362. <p>Dioxus is also really fast. Because Dioxus splits the diff and patch phase, it's able to make all the edits to the RealDOM in a very short amount of time (less than a single frame) making rendering very snappy. It also allows Dioxus to cancel large diffing operations if higher priority work comes in while it's diffing.</p>
  3363. <p>This little demo serves to show exactly how a Renderer would need to process a mutation stream to build UIs.</p>
  3364. <h2 id="event-loop"><a class="header" href="#event-loop">Event loop</a></h2>
  3365. <p>Like most GUIs, Dioxus relies on an event loop to progress the VirtualDOM. The VirtualDOM itself can produce events as well, so it's important for your custom renderer can handle those too.</p>
  3366. <p>The code for the WebSys implementation is straightforward, so we'll add it here to demonstrate how simple an event loop is:</p>
  3367. <pre><code class="language-rust ignore">pub async fn run(&amp;mut self) -&gt; dioxus_core::error::Result&lt;()&gt; {
  3368. // Push the body element onto the WebsysDom's stack machine
  3369. let mut websys_dom = crate::new::WebsysDom::new(prepare_websys_dom());
  3370. websys_dom.stack.push(root_node);
  3371. // Rebuild or hydrate the virtualdom
  3372. let mutations = self.internal_dom.rebuild();
  3373. websys_dom.apply_mutations(mutations);
  3374. // Wait for updates from the real dom and progress the virtual dom
  3375. loop {
  3376. let user_input_future = websys_dom.wait_for_event();
  3377. let internal_event_future = self.internal_dom.wait_for_work();
  3378. match select(user_input_future, internal_event_future).await {
  3379. Either::Left((_, _)) =&gt; {
  3380. let mutations = self.internal_dom.work_with_deadline(|| false);
  3381. websys_dom.apply_mutations(mutations);
  3382. },
  3383. Either::Right((event, _)) =&gt; websys_dom.handle_event(event),
  3384. }
  3385. // render
  3386. }
  3387. }
  3388. </code></pre>
  3389. <p>It's important to decode what the real events are for your event system into Dioxus' synthetic event system (synthetic meaning abstracted). This simply means matching your event type and creating a Dioxus <code>UserEvent</code> type. Right now, the virtual event system is modeled almost entirely around the HTML spec, but we are interested in slimming it down.</p>
  3390. <pre><code class="language-rust ignore">fn virtual_event_from_websys_event(event: &amp;web_sys::Event) -&gt; VirtualEvent {
  3391. match event.type_().as_str() {
  3392. &quot;keydown&quot; =&gt; {
  3393. let event: web_sys::KeyboardEvent = event.clone().dyn_into().unwrap();
  3394. UserEvent::KeyboardEvent(UserEvent {
  3395. scope_id: None,
  3396. priority: EventPriority::Medium,
  3397. name: &quot;keydown&quot;,
  3398. // This should be whatever element is focused
  3399. element: Some(ElementId(0)),
  3400. data: Arc::new(KeyboardData{
  3401. char_code: event.char_code(),
  3402. key: event.key(),
  3403. key_code: event.key_code(),
  3404. alt_key: event.alt_key(),
  3405. ctrl_key: event.ctrl_key(),
  3406. meta_key: event.meta_key(),
  3407. shift_key: event.shift_key(),
  3408. location: event.location(),
  3409. repeat: event.repeat(),
  3410. which: event.which(),
  3411. })
  3412. })
  3413. }
  3414. _ =&gt; todo!()
  3415. }
  3416. }
  3417. </code></pre>
  3418. <h2 id="custom-raw-elements"><a class="header" href="#custom-raw-elements">Custom raw elements</a></h2>
  3419. <p>If you need to go as far as relying on custom elements/attributes for your renderer – you totally can. This still enables you to use Dioxus' reactive nature, component system, shared state, and other features, but will ultimately generate different nodes. All attributes and listeners for the HTML and SVG namespace are shuttled through helper structs that essentially compile away. You can drop in your elements any time you want, with little hassle. However, you must be sure your renderer can handle the new namespace.</p>
  3420. <p>For more examples and information on how to create custom namespaces, see the <a href="https://github.com/DioxusLabs/dioxus/blob/master/packages/html/README.md#how-to-extend-it"><code>dioxus_html</code> crate</a>.</p>
  3421. <h1 id="native-core"><a class="header" href="#native-core">Native Core</a></h1>
  3422. <p>If you are creating a renderer in rust, the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/native-core">native-core</a> crate provides some utilities to implement a renderer. It provides an abstraction over Mutations and Templates and contains helpers that can handle the layout and text editing for you.</p>
  3423. <h2 id="the-realdom"><a class="header" href="#the-realdom">The RealDom</a></h2>
  3424. <p>The <code>RealDom</code> is a higher-level abstraction over updating the Dom. It uses an entity component system to manage the state of nodes. This system allows you to modify insert and modify arbitrary components on nodes. On top of this, the RealDom provides a way to manage a tree of nodes, and the State trait provides a way to automatically add and update these components when the tree is modified. It also provides a way to apply <code>Mutations</code> to the RealDom.</p>
  3425. <h3 id="example"><a class="header" href="#example">Example</a></h3>
  3426. <p>Let's build a toy renderer with borders, size, and text color.
  3427. Before we start let's take a look at an example element we can render:</p>
  3428. <pre><pre class="playground"><code class="language-rust edition2018">
  3429. <span class="boring">#![allow(unused)]
  3430. </span><span class="boring">fn main() {
  3431. </span>cx.render(rsx!{
  3432. div{
  3433. color: &quot;red&quot;,
  3434. p{
  3435. border: &quot;1px solid black&quot;,
  3436. &quot;hello world&quot;
  3437. }
  3438. }
  3439. })
  3440. <span class="boring">}
  3441. </span></code></pre></pre>
  3442. <p>In this tree, the color depends on the parent's color. The layout depends on the children's layout, the current text, and the text size. The border depends on only the current node.</p>
  3443. <p>In the following diagram arrows represent dataflow:</p>
  3444. <p><a href="https://mermaid.live/edit#pako:eNqllV1vgjAUhv8K6W4wkQVa2QdLdrHsdlfukmSptEhjoaSWqTH-9xVwONAKst70g5739JzzlO5BJAgFAYi52EQJlsr6fAszS7d1sVhKnCdWJDJFt6peLVs5-9owohK7HFrVcFJ_pxnpmK8VVvRkTJikkWIiaxy1dhP23bUwW1WW5WbPrrqJ4ziR4EJ6dtVN2ls5y1ZztePUcrWZFCvqVEcPPDffvlyS1XoLIQnVgnVvVPR6FU9Zc-6dV453ojjOPbuetRJ57gIeXQR3cez7rjtteZyZQ2j5MqmjqwE0ZW0VKx9RKtgpFewp1aw3sXXFy6TWgiYlv8mfq1scD8ofbBCAfQg8_AMBOAyBxzEIwA4CxgQ99QbQkjnD2KT7_CfxGF8_9WXQEsq5sDZCcjICOXRCri4h6r3NA38Q6Jdi1EOx5w3DGDYYI6MUvJFjM3VoGHUeGoMd6mBnDmh2E3fo7O4Yhf0x4OkBmIKUyhQzol_GfbkcApXQlIYg0EOC5SoEYXbQ-3ChxHyXRSBQsqBTUOREx_7OsAY3BUGM-VqvUsKUkB_1U6vf05gtweEHTk4_HQ"><img src="https://mermaid.ink/img/pako:eNqllV1vgjAUhv8K6W4wkQVa2QdLdrHsdlfukmSptEhjoaSWqTH-9xVwONAKst70g5739JzzlO5BJAgFAYi52EQJlsr6fAszS7d1sVhKnCdWJDJFt6peLVs5-9owohK7HFrVcFJ_pxnpmK8VVvRkTJikkWIiaxy1dhP23bUwW1WW5WbPrrqJ4ziR4EJ6dtVN2ls5y1ZztePUcrWZFCvqVEcPPDffvlyS1XoLIQnVgnVvVPR6FU9Zc-6dV453ojjOPbuetRJ57gIeXQR3cez7rjtteZyZQ2j5MqmjqwE0ZW0VKx9RKtgpFewp1aw3sXXFy6TWgiYlv8mfq1scD8ofbBCAfQg8_AMBOAyBxzEIwA4CxgQ99QbQkjnD2KT7_CfxGF8_9WXQEsq5sDZCcjICOXRCri4h6r3NA38Q6Jdi1EOx5w3DGDYYI6MUvJFjM3VoGHUeGoMd6mBnDmh2E3fo7O4Yhf0x4OkBmIKUyhQzol_GfbkcApXQlIYg0EOC5SoEYXbQ-3ChxHyXRSBQsqBTUOREx_7OsAY3BUGM-VqvUsKUkB_1U6vf05gtweEHTk4_HQ?type=png" alt="" /></a></p>
  3445. <p>To help in building a Dom, native-core provides the State trait and a RealDom struct. The State trait provides a way to describe how states in a node depend on other states in its relatives. By describing how to update a single node from its relations, native-core will derive a way to update the states of all nodes for you. Once you have a state you can provide it as a generic to RealDom. RealDom provides all of the methods to interact and update your new dom.</p>
  3446. <p>Native Core cannot create all of the required methods for the State trait, but it can derive some of them. To implement the State trait, you must implement the following methods and let the <code>#[partial_derive_state]</code> macro handle the rest:</p>
  3447. <pre><code class="language-rust ignore">// All states must derive Component (https://docs.rs/shipyard/latest/shipyard/derive.Component.html)
  3448. // They also must implement Default or provide a custom implementation of create in the State trait
  3449. #[derive(Default, Component)]
  3450. struct MyState;
  3451. /// Derive some of the boilerplate for the State implementation
  3452. #[partial_derive_state]
  3453. impl State for MyState {
  3454. // The states of the parent nodes this state depends on
  3455. type ParentDependencies = ();
  3456. // The states of the child nodes this state depends on
  3457. type ChildDependencies = (Self,);
  3458. // The states of the current node this state depends on
  3459. type NodeDependencies = ();
  3460. // The parts of the current text, element, or placeholder node in the tree that this state depends on
  3461. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; = NodeMaskBuilder::new();
  3462. // How to update the state of the current node based on the state of the parent nodes, child nodes, and the current node
  3463. // Returns true if the node was updated and false if the node was not updated
  3464. fn update&lt;'a&gt;(
  3465. &amp;mut self,
  3466. // The view of the current node limited to the parts this state depends on
  3467. _node_view: NodeView&lt;()&gt;,
  3468. // The state of the current node that this state depends on
  3469. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  3470. // The state of the parent nodes that this state depends on
  3471. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3472. // The state of the child nodes that this state depends on
  3473. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3474. // The context of the current node used to pass global state into the tree
  3475. _context: &amp;SendAnyMap,
  3476. ) -&gt; bool {
  3477. todo!()
  3478. }
  3479. // partial_derive_state will generate a default implementation of all the other methods
  3480. }
  3481. </code></pre>
  3482. <p>Lets take a look at how to implement the State trait for a simple renderer.</p>
  3483. <pre><pre class="playground"><code class="language-rust edition2018">
  3484. <span class="boring">#![allow(unused)]
  3485. </span><span class="boring">fn main() {
  3486. </span>struct FontSize(f64);
  3487. // All states need to derive Component
  3488. #[derive(Default, Debug, Copy, Clone, Component)]
  3489. struct Size(f64, f64);
  3490. /// Derive some of the boilerplate for the State implementation
  3491. #[partial_derive_state]
  3492. impl State for Size {
  3493. type ParentDependencies = ();
  3494. // The size of the current node depends on the size of its children
  3495. type ChildDependencies = (Self,);
  3496. type NodeDependencies = ();
  3497. // Size only cares about the width, height, and text parts of the current node
  3498. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; = NodeMaskBuilder::new()
  3499. // Get access to the width and height attributes
  3500. .with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;width&quot;, &quot;height&quot;]))
  3501. // Get access to the text of the node
  3502. .with_text();
  3503. fn update&lt;'a&gt;(
  3504. &amp;mut self,
  3505. node_view: NodeView&lt;()&gt;,
  3506. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  3507. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3508. children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3509. context: &amp;SendAnyMap,
  3510. ) -&gt; bool {
  3511. let font_size = context.get::&lt;FontSize&gt;().unwrap().0;
  3512. let mut width;
  3513. let mut height;
  3514. if let Some(text) = node_view.text() {
  3515. // if the node has text, use the text to size our object
  3516. width = text.len() as f64 * font_size;
  3517. height = font_size;
  3518. } else {
  3519. // otherwise, the size is the maximum size of the children
  3520. width = children
  3521. .iter()
  3522. .map(|(item,)| item.0)
  3523. .reduce(|accum, item| if accum &gt;= item { accum } else { item })
  3524. .unwrap_or(0.0);
  3525. height = children
  3526. .iter()
  3527. .map(|(item,)| item.1)
  3528. .reduce(|accum, item| if accum &gt;= item { accum } else { item })
  3529. .unwrap_or(0.0);
  3530. }
  3531. // if the node contains a width or height attribute it overrides the other size
  3532. for a in node_view.attributes().into_iter().flatten() {
  3533. match &amp;*a.attribute.name {
  3534. &quot;width&quot; =&gt; width = a.value.as_float().unwrap(),
  3535. &quot;height&quot; =&gt; height = a.value.as_float().unwrap(),
  3536. // because Size only depends on the width and height, no other attributes will be passed to the member
  3537. _ =&gt; panic!(),
  3538. }
  3539. }
  3540. // to determine what other parts of the dom need to be updated we return a boolean that marks if this member changed
  3541. let changed = (width != self.0) || (height != self.1);
  3542. *self = Self(width, height);
  3543. changed
  3544. }
  3545. }
  3546. #[derive(Debug, Clone, Copy, PartialEq, Default, Component)]
  3547. struct TextColor {
  3548. r: u8,
  3549. g: u8,
  3550. b: u8,
  3551. }
  3552. #[partial_derive_state]
  3553. impl State for TextColor {
  3554. // TextColor depends on the TextColor part of the parent
  3555. type ParentDependencies = (Self,);
  3556. type ChildDependencies = ();
  3557. type NodeDependencies = ();
  3558. // TextColor only cares about the color attribute of the current node
  3559. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; =
  3560. // Get access to the color attribute
  3561. NodeMaskBuilder::new().with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;color&quot;]));
  3562. fn update&lt;'a&gt;(
  3563. &amp;mut self,
  3564. node_view: NodeView&lt;()&gt;,
  3565. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  3566. parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3567. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3568. _context: &amp;SendAnyMap,
  3569. ) -&gt; bool {
  3570. // TextColor only depends on the color tag, so getting the first tag is equivilent to looking through all tags
  3571. let new = match node_view
  3572. .attributes()
  3573. .and_then(|mut attrs| attrs.next())
  3574. .and_then(|attr| attr.value.as_text())
  3575. {
  3576. // if there is a color tag, translate it
  3577. Some(&quot;red&quot;) =&gt; TextColor { r: 255, g: 0, b: 0 },
  3578. Some(&quot;green&quot;) =&gt; TextColor { r: 0, g: 255, b: 0 },
  3579. Some(&quot;blue&quot;) =&gt; TextColor { r: 0, g: 0, b: 255 },
  3580. Some(color) =&gt; panic!(&quot;unknown color {color}&quot;),
  3581. // otherwise check if the node has a parent and inherit that color
  3582. None =&gt; match parent {
  3583. Some((parent,)) =&gt; *parent,
  3584. None =&gt; Self::default(),
  3585. },
  3586. };
  3587. // check if the member has changed
  3588. let changed = new != *self;
  3589. *self = new;
  3590. changed
  3591. }
  3592. }
  3593. #[derive(Debug, Clone, Copy, PartialEq, Default, Component)]
  3594. struct Border(bool);
  3595. #[partial_derive_state]
  3596. impl State for Border {
  3597. // TextColor depends on the TextColor part of the parent
  3598. type ParentDependencies = (Self,);
  3599. type ChildDependencies = ();
  3600. type NodeDependencies = ();
  3601. // Border does not depended on any other member in the current node
  3602. const NODE_MASK: NodeMaskBuilder&lt;'static&gt; =
  3603. // Get access to the border attribute
  3604. NodeMaskBuilder::new().with_attrs(AttributeMaskBuilder::Some(&amp;[&quot;border&quot;]));
  3605. fn update&lt;'a&gt;(
  3606. &amp;mut self,
  3607. node_view: NodeView&lt;()&gt;,
  3608. _node: &lt;Self::NodeDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;,
  3609. _parent: Option&lt;&lt;Self::ParentDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3610. _children: Vec&lt;&lt;Self::ChildDependencies as Dependancy&gt;::ElementBorrowed&lt;'a&gt;&gt;,
  3611. _context: &amp;SendAnyMap,
  3612. ) -&gt; bool {
  3613. // check if the node contians a border attribute
  3614. let new = Self(
  3615. node_view
  3616. .attributes()
  3617. .and_then(|mut attrs| attrs.next().map(|a| a.attribute.name == &quot;border&quot;))
  3618. .is_some(),
  3619. );
  3620. // check if the member has changed
  3621. let changed = new != *self;
  3622. *self = new;
  3623. changed
  3624. }
  3625. }
  3626. <span class="boring">}
  3627. </span></code></pre></pre>
  3628. <p>Now that we have our state, we can put it to use in our RealDom. We can update the RealDom with apply_mutations to update the structure of the dom (adding, removing, and changing properties of nodes) and then update_state to update the States for each of the nodes that changed.</p>
  3629. <pre><pre class="playground"><code class="language-rust edition2018">fn main() -&gt; Result&lt;(), Box&lt;dyn std::error::Error&gt;&gt; {
  3630. fn app(cx: Scope) -&gt; Element {
  3631. let count = use_state(cx, || 0);
  3632. use_future(cx, (count,), |(count,)| async move {
  3633. loop {
  3634. tokio::time::sleep(std::time::Duration::from_secs(1)).await;
  3635. count.set(*count + 1);
  3636. }
  3637. });
  3638. cx.render(rsx! {
  3639. div{
  3640. color: &quot;red&quot;,
  3641. &quot;{count}&quot;
  3642. }
  3643. })
  3644. }
  3645. // create the vdom, the real_dom, and the binding layer between them
  3646. let mut vdom = VirtualDom::new(app);
  3647. let mut rdom: RealDom = RealDom::new([
  3648. Border::to_type_erased(),
  3649. TextColor::to_type_erased(),
  3650. Size::to_type_erased(),
  3651. ]);
  3652. let mut dioxus_intigration_state = DioxusState::create(&amp;mut rdom);
  3653. let mutations = vdom.rebuild();
  3654. // update the structure of the real_dom tree
  3655. dioxus_intigration_state.apply_mutations(&amp;mut rdom, mutations);
  3656. let mut ctx = SendAnyMap::new();
  3657. // set the font size to 3.3
  3658. ctx.insert(FontSize(3.3));
  3659. // update the State for nodes in the real_dom tree
  3660. let _to_rerender = rdom.update_state(ctx);
  3661. // we need to run the vdom in a async runtime
  3662. tokio::runtime::Builder::new_current_thread()
  3663. .enable_all()
  3664. .build()?
  3665. .block_on(async {
  3666. loop {
  3667. // wait for the vdom to update
  3668. vdom.wait_for_work().await;
  3669. // get the mutations from the vdom
  3670. let mutations = vdom.render_immediate();
  3671. // update the structure of the real_dom tree
  3672. dioxus_intigration_state.apply_mutations(&amp;mut rdom, mutations);
  3673. // update the state of the real_dom tree
  3674. let mut ctx = SendAnyMap::new();
  3675. // set the font size to 3.3
  3676. ctx.insert(FontSize(3.3));
  3677. let _to_rerender = rdom.update_state(ctx);
  3678. // render...
  3679. rdom.traverse_depth_first(|node| {
  3680. let indent = &quot; &quot;.repeat(node.height() as usize);
  3681. let color = *node.get::&lt;TextColor&gt;().unwrap();
  3682. let size = *node.get::&lt;Size&gt;().unwrap();
  3683. let border = *node.get::&lt;Border&gt;().unwrap();
  3684. let id = node.id();
  3685. let node = node.node_type();
  3686. let node_type = &amp;*node;
  3687. println!(&quot;{indent}{id:?} {color:?} {size:?} {border:?} {node_type:?}&quot;);
  3688. });
  3689. }
  3690. })
  3691. }
  3692. </code></pre></pre>
  3693. <h2 id="layout"><a class="header" href="#layout">Layout</a></h2>
  3694. <p>For most platforms, the layout of the Elements will stay the same. The <a href="https://docs.rs/dioxus-native-core/latest/dioxus_native_core/layout_attributes/index.html">layout_attributes</a> module provides a way to apply HTML attributes a <a href="https://docs.rs/taffy/latest/taffy/index.html">Taffy</a> layout style.</p>
  3695. <h2 id="text-editing"><a class="header" href="#text-editing">Text Editing</a></h2>
  3696. <p>To make it easier to implement text editing in rust renderers, <code>native-core</code> also contains a renderer-agnostic cursor system. The cursor can handle text editing, selection, and movement with common keyboard shortcuts integrated.</p>
  3697. <pre><pre class="playground"><code class="language-rust edition2018">
  3698. <span class="boring">#![allow(unused)]
  3699. </span><span class="boring">fn main() {
  3700. </span>fn text_editing() {
  3701. let mut cursor = Cursor::default();
  3702. let mut text = String::new();
  3703. // handle keyboard input with a max text length of 10
  3704. cursor.handle_input(
  3705. &amp;Code::ArrowRight,
  3706. &amp;Key::ArrowRight,
  3707. &amp;Modifiers::empty(),
  3708. &amp;mut text,
  3709. 10,
  3710. );
  3711. // mannually select text between characters 0-5 on the first line (this could be from dragging with a mouse)
  3712. cursor.start = Pos::new(0, 0);
  3713. cursor.end = Some(Pos::new(5, 0));
  3714. // delete the selected text and move the cursor to the start of the selection
  3715. cursor.delete_selection(&amp;mut text);
  3716. }
  3717. <span class="boring">}
  3718. </span></code></pre></pre>
  3719. <h2 id="conclusion-1"><a class="header" href="#conclusion-1">Conclusion</a></h2>
  3720. <p>That should be it! You should have nearly all the knowledge required on how to implement your renderer. We're super interested in seeing Dioxus apps brought to custom desktop renderers, mobile renderers, video game UI, and even augmented reality! If you're interested in contributing to any of these projects, don't be afraid to reach out or join the <a href="https://discord.gg/XgGxMSkvUM">community</a>.</p>
  3721. <div style="break-before: page; page-break-before: always;"></div><h1 id="contributing"><a class="header" href="#contributing">Contributing</a></h1>
  3722. <p>Development happens in the <a href="https://github.com/DioxusLabs/dioxus">Dioxus GitHub repository</a>. If you've found a bug or have an idea for a feature, please submit an issue (but first check if someone hasn't <a href="https://github.com/DioxusLabs/dioxus/issues">done it already</a>).</p>
  3723. <p><a href="https://github.com/DioxusLabs/dioxus/discussions">GitHub discussions</a> can be used as a place to ask for help or talk about features. You can also join <a href="https://discord.gg/XgGxMSkvUM">our Discord channel</a> where some development discussion happens.</p>
  3724. <h2 id="improving-docs"><a class="header" href="#improving-docs">Improving Docs</a></h2>
  3725. <p>If you'd like to improve the docs, PRs are welcome! Both Rust docs (<a href="https://github.com/DioxusLabs/dioxus/tree/master/packages">source</a>) and this guide (<a href="https://github.com/DioxusLabs/dioxus/tree/master/docs/guide">source</a>) can be found in the GitHub repo.</p>
  3726. <h2 id="working-on-the-ecosystem"><a class="header" href="#working-on-the-ecosystem">Working on the Ecosystem</a></h2>
  3727. <p>Part of what makes React great is the rich ecosystem. We'd like the same for Dioxus! So if you have a library in mind that you'd like to write and many people would benefit from, it will be appreciated. You can <a href="https://www.npmjs.com/search?q=keywords:react-component">browse npm.js</a> for inspiration. Once you are done, add your library to the <a href="https://github.com/DioxusLabs/awesome-dioxus">awesome dioxus</a> list or share it in the <code>#I-made-a-thing</code> channel on <a href="https://discord.gg/XgGxMSkvUM">Discord</a>.</p>
  3728. <h2 id="bugs--features"><a class="header" href="#bugs--features">Bugs &amp; Features</a></h2>
  3729. <p>If you've fixed <a href="https://github.com/DioxusLabs/dioxus/issues">an open issue</a>, feel free to submit a PR! You can also take a look at <a href="contributing/./roadmap.html">the roadmap</a> and work on something in there. Consider <a href="https://discord.gg/XgGxMSkvUM">reaching out</a> to the team first to make sure everyone's on the same page, and you don't do useless work!</p>
  3730. <p>All pull requests (including those made by a team member) must be approved by at least one other team member.
  3731. Larger, more nuanced decisions about design, architecture, breaking changes, trade-offs, etc. are made by team consensus.</p>
  3732. <h2 id="tools"><a class="header" href="#tools">Tools</a></h2>
  3733. <p>The following tools can be helpful when developing Dioxus. Many of these tools are used in the CI pipeline. Running them locally before submitting a PR instead of waiting for CI can save time.</p>
  3734. <ul>
  3735. <li>All code is tested with <a href="https://doc.rust-lang.org/cargo/commands/cargo-test.html">cargo test</a></li>
  3736. </ul>
  3737. <pre><code class="language-sh">cargo fmt --all
  3738. </code></pre>
  3739. <ul>
  3740. <li>All code is formatted with <a href="https://github.com/rust-lang/rustfmt">rustfmt</a></li>
  3741. </ul>
  3742. <pre><code class="language-sh">cargo check --workspace --examples --tests
  3743. </code></pre>
  3744. <ul>
  3745. <li>All code is linted with <a href="https://doc.rust-lang.org/clippy/">Clippy</a></li>
  3746. </ul>
  3747. <pre><code class="language-sh">cargo clippy --workspace --examples --tests -- -D warnings
  3748. </code></pre>
  3749. <ul>
  3750. <li>Crates that use unsafe are checked for undefined behavior with <a href="https://github.com/rust-lang/miri">MIRI</a>. MIRI can be helpful to debug what unsafe code is causing issues. Only code that does not interact with system calls can be checked with MIRI. Currently, this is used for the two MIRI tests in <code>dioxus-core</code> and <code>dioxus-native-core</code>.</li>
  3751. </ul>
  3752. <pre><code class="language-sh">cargo miri test --package dioxus-core --test miri_stress
  3753. cargo miri test --package dioxus-native-core --test miri_native
  3754. </code></pre>
  3755. <ul>
  3756. <li><a href="https://rust-analyzer.github.io/">Rust analyzer</a> can be very helpful for quick feedback in your IDE.</li>
  3757. </ul>
  3758. <div style="break-before: page; page-break-before: always;"></div><h1 id="project-struture"><a class="header" href="#project-struture">Project Struture</a></h1>
  3759. <p>There are many packages in the Dioxus organization. This document will help you understand the purpose of each package and how they fit together.</p>
  3760. <h2 id="renderers"><a class="header" href="#renderers">Renderers</a></h2>
  3761. <ul>
  3762. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/desktop">Desktop</a>: A Render that Runs Dioxus applications natively, but renders them with the system webview</li>
  3763. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/mobile">Mobile</a>: A Render that Runs Dioxus applications natively, but renders them with the system webview. This is currently a copy of the desktop render</li>
  3764. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/Web">Web</a>: Renders Dioxus applications in the browser by compiling to WASM and manipulating the DOM</li>
  3765. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/liveview">Liveview</a>: A Render that Runs on the server, and renders using a websocket proxy in the browser</li>
  3766. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/rink">Rink</a>: A Renderer that renders a HTML-like tree into a terminal</li>
  3767. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/dioxus-tui">TUI</a>: A Renderer that uses Rink to render a Dioxus application in a terminal</li>
  3768. <li><a href="https://github.com/DioxusLabs/blitz/tree/master/blitz-core">Blitz-Core</a>: An experimental native renderer that renders a HTML-like tree using WGPU.</li>
  3769. <li><a href="https://github.com/DioxusLabs/blitz">Blitz</a>: An experimental native renderer that uses Blitz-Core to render a Dioxus application using WGPU.</li>
  3770. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/ssr">SSR</a>: A Render that Runs Dioxus applications on the server, and renders them to HTML</li>
  3771. </ul>
  3772. <h2 id="state-managementhooks"><a class="header" href="#state-managementhooks">State Management/Hooks</a></h2>
  3773. <ul>
  3774. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/hooks">Hooks</a>: A collection of common hooks for Dioxus applications</li>
  3775. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/signals">Signals</a>: A experimental state management library for Dioxus applications. This currently contains a <code>Copy</code> version of UseRef</li>
  3776. <li><a href="https://github.com/DioxusLabs/dioxus-std">Dioxus STD</a>: A collection of platform agnostic hooks to interact with system interfaces (The clipboard, camera, etc.).</li>
  3777. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/fermi">Fermi</a>: A global state management library for Dioxus applications.
  3778. <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/router">Router</a>: A client-side router for Dioxus applications</li>
  3779. </ul>
  3780. <h2 id="core-utilities"><a class="header" href="#core-utilities">Core utilities</a></h2>
  3781. <ul>
  3782. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/core">core</a>: The core virtual dom implementation every Dioxus application uses
  3783. <ul>
  3784. <li>You can read more about the archetecture of the core <a href="https://dioxuslabs.com/blog/templates-diffing/">in this blog post</a> and the <a href="contributing/../custom_renderer/index.html">custom renderer section of the guide</a></li>
  3785. </ul>
  3786. </li>
  3787. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/RSX">RSX</a>: The core parsing for RSX used for hot reloading, autoformatting, and the macro</li>
  3788. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/core-macro">core-macro</a>: The rsx! macro used to write Dioxus applications. (This is a wrapper over the RSX crate)</li>
  3789. <li><a href="https://github.com/DioxusLabs/dioxus-html-macro">HTML macro</a>: A html-like alternative to the RSX macro</li>
  3790. </ul>
  3791. <h2 id="native-renderer-utilities"><a class="header" href="#native-renderer-utilities">Native Renderer Utilities</a></h2>
  3792. <ul>
  3793. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/native-core">native-core</a>: Incrementally computed tree of states (mostly styles)
  3794. <ul>
  3795. <li>You can read more about how native-core can help you build native renderers in the <a href="contributing/../custom_renderer/index.html#native-core">custom renderer section of the guide</a></li>
  3796. </ul>
  3797. </li>
  3798. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/native-core-macro">native-core-macro</a>: A helper macro for native core</li>
  3799. <li><a href="https://github.com/DioxusLabs/taffy">Taffy</a>: Layout engine powering Blitz-Core, Rink, and Bevy UI</li>
  3800. </ul>
  3801. <h2 id="web-renderer-tooling"><a class="header" href="#web-renderer-tooling">Web renderer tooling</a></h2>
  3802. <ul>
  3803. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/html">HTML</a>: defines html specific elements, events, and attributes</li>
  3804. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/interpreter">Interpreter</a>: defines browser bindings used by the web and desktop renderers</li>
  3805. </ul>
  3806. <h2 id="developer-tooling"><a class="header" href="#developer-tooling">Developer tooling</a></h2>
  3807. <ul>
  3808. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/hot-reload">hot-reload</a>: Macro that uses the RSX crate to hot reload static parts of any rsx! macro. This macro works with any non-web renderer with an <a href="https://crates.io/crates/dioxus-hot-reload">integration</a></li>
  3809. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/autofmt">autofmt</a>: Formats RSX code</li>
  3810. <li><a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/RSX-rosetta">rsx-rosetta</a>: Handles conversion between HTML and RSX</li>
  3811. <li><a href="https://github.com/DioxusLabs/cli">CLI</a>: A Command Line Interface and VSCode extension to assist with Dioxus usage</li>
  3812. </ul>
  3813. <div style="break-before: page; page-break-before: always;"></div><h1 id="walkthrough-of-the-hello-world-example-internals"><a class="header" href="#walkthrough-of-the-hello-world-example-internals">Walkthrough of the Hello World Example Internals</a></h1>
  3814. <p>This walkthrough will take you through the internals of the Hello World example program. It will explain how major parts of Dioxus internals interact with each other to take the readme example from a source file to a running application. This guide should serve as a high-level overview of the internals of Dioxus. It is not meant to be a comprehensive guide.</p>
  3815. <h2 id="the-source-file"><a class="header" href="#the-source-file">The Source File</a></h2>
  3816. <p>We start will a hello world program. This program renders a desktop app with the text &quot;Hello World&quot; in a webview.</p>
  3817. <pre><pre class="playground"><code class="language-rust edition2018">//! Example: README.md showcase
  3818. //!
  3819. //! The example from the README.md.
  3820. use dioxus::prelude::*;
  3821. fn main() {
  3822. dioxus_desktop::launch(app);
  3823. }
  3824. fn app(cx: Scope) -&gt; Element {
  3825. let mut count = use_state(cx, || 0);
  3826. cx.render(rsx! {
  3827. h1 { &quot;High-Five counter: {count}&quot; }
  3828. button { onclick: move |_| count += 1, &quot;Up high!&quot; }
  3829. button { onclick: move |_| count -= 1, &quot;Down low!&quot; }
  3830. })
  3831. }
  3832. </code></pre></pre>
  3833. <p><a href="https://mermaid.live/edit#pako:eNqNkT1vwyAQhv8KvSlR48HphtQtqjK0S6tuSBGBS0CxwcJHk8rxfy_YVqxKVdR3ug_u4YXrQHmNwOFQ-bMyMhB7fReOJbVxfwyyMSy0l7GSpW1ARda727ksUy5MuSyKgvBC5ULA1h5N8WK_kCkfHWHgrBuiXsBynrvdsY9E3u1iM_eyvFOVVadMnELOap-o1911JLPHZ1b-YqLTc3LjTt7WifTZMJPsPdx1ov3Z_ellfcdL8R8vmTy5eUqsTUpZ-vzZzjAEK6gx1NLqtJwuNwSQwRoF8BRqGU4ChOvTORnJf3w7BZxCxBXERkvCjZXpQTXwg6zaVEVtyYe3cdvD0vsf4bucgw"><img src="https://mermaid.ink/img/pako:eNqNkT1vwyAQhv8KvSlR48HphtQtqjK0S6tuSBGBS0CxwcJHk8rxfy_YVqxKVdR3ug_u4YXrQHmNwOFQ-bMyMhB7fReOJbVxfwyyMSy0l7GSpW1ARda727ksUy5MuSyKgvBC5ULA1h5N8WK_kCkfHWHgrBuiXsBynrvdsY9E3u1iM_eyvFOVVadMnELOap-o1911JLPHZ1b-YqLTc3LjTt7WifTZMJPsPdx1ov3Z_ellfcdL8R8vmTy5eUqsTUpZ-vzZzjAEK6gx1NLqtJwuNwSQwRoF8BRqGU4ChOvTORnJf3w7BZxCxBXERkvCjZXpQTXwg6zaVEVtyYe3cdvD0vsf4bucgw?type=png" alt="" /></a></p>
  3834. <h2 id="the-rsx-macro"><a class="header" href="#the-rsx-macro">The rsx! Macro</a></h2>
  3835. <p>Before the Rust compiler runs the program, it will expand all macros. Here is what the hello world example looks like expanded:</p>
  3836. <pre><pre class="playground"><code class="language-rust edition2018">use dioxus::prelude::*;
  3837. fn main() {
  3838. dioxus_desktop::launch(app);
  3839. }
  3840. fn app(cx: Scope) -&gt; Element {
  3841. let mut count = use_state(cx, || 0);
  3842. cx.render(
  3843. // rsx expands to LazyNodes::new
  3844. ::dioxus::core::LazyNodes::new(
  3845. move |__cx: &amp;::dioxus::core::ScopeState| -&gt; ::dioxus::core::VNode {
  3846. // The template is every static part of the rsx
  3847. static TEMPLATE: ::dioxus::core::Template = ::dioxus::core::Template {
  3848. // This is the source location of the rsx that generated this template. This is used to make hot rsx reloading work. Hot rsx reloading just replaces the template with a new one generated from the rsx by the CLI.
  3849. name: &quot;examples\\readme.rs:14:15:250&quot;,
  3850. // The root nodes are the top level nodes of the rsx
  3851. roots: &amp;[
  3852. // The h1 node
  3853. ::dioxus::core::TemplateNode::Element {
  3854. // Find the built in h1 tag in the dioxus_elements crate exported by the dioxus html crate
  3855. tag: dioxus_elements::h1::TAG_NAME,
  3856. namespace: dioxus_elements::h1::NAME_SPACE,
  3857. attrs: &amp;[],
  3858. // The children of the h1 node
  3859. children: &amp;[
  3860. // The dynamic count text node
  3861. // Any nodes that are dynamic have a dynamic placeholder with a unique index
  3862. ::dioxus::core::TemplateNode::DynamicText {
  3863. // This index is used to find what element in `dynamic_nodes` to use instead of the placeholder
  3864. id: 0usize,
  3865. },
  3866. ],
  3867. },
  3868. // The up high button node
  3869. ::dioxus::core::TemplateNode::Element {
  3870. tag: dioxus_elements::button::TAG_NAME,
  3871. namespace: dioxus_elements::button::NAME_SPACE,
  3872. attrs: &amp;[
  3873. // The dynamic onclick listener attribute
  3874. // Any attributes that are dynamic have a dynamic placeholder with a unique index.
  3875. ::dioxus::core::TemplateAttribute::Dynamic {
  3876. // Similar to dynamic nodes, dynamic attributes have a unique index used to find the attribute in `dynamic_attrs` to use instead of the placeholder
  3877. id: 0usize,
  3878. },
  3879. ],
  3880. children: &amp;[::dioxus::core::TemplateNode::Text { text: &quot;Up high!&quot; }],
  3881. },
  3882. // The down low button node
  3883. ::dioxus::core::TemplateNode::Element {
  3884. tag: dioxus_elements::button::TAG_NAME,
  3885. namespace: dioxus_elements::button::NAME_SPACE,
  3886. attrs: &amp;[
  3887. // The dynamic onclick listener attribute
  3888. ::dioxus::core::TemplateAttribute::Dynamic { id: 1usize },
  3889. ],
  3890. children: &amp;[::dioxus::core::TemplateNode::Text { text: &quot;Down low!&quot; }],
  3891. },
  3892. ],
  3893. // Node paths is a list of paths to every dynamic node in the rsx
  3894. node_paths: &amp;[
  3895. // The first node path is the path to the dynamic node with an id of 0 (the count text node)
  3896. &amp;[
  3897. // Go to the index 0 root node
  3898. 0u8,
  3899. //
  3900. // Go to the first child of the root node
  3901. 0u8,
  3902. ],
  3903. ],
  3904. // Attr paths is a list of paths to every dynamic attribute in the rsx
  3905. attr_paths: &amp;[
  3906. // The first attr path is the path to the dynamic attribute with an id of 0 (the up high button onclick listener)
  3907. &amp;[
  3908. // Go to the index 1 root node
  3909. 1u8,
  3910. ],
  3911. // The second attr path is the path to the dynamic attribute with an id of 1 (the down low button onclick listener)
  3912. &amp;[
  3913. // Go to the index 2 root node
  3914. 2u8,
  3915. ],
  3916. ],
  3917. };
  3918. // The VNode is a reference to the template with the dynamic parts of the rsx
  3919. ::dioxus::core::VNode {
  3920. parent: None,
  3921. key: None,
  3922. // The static template this node will use. The template is stored in a Cell so it can be replaced with a new template when hot rsx reloading is enabled
  3923. template: std::cell::Cell::new(TEMPLATE),
  3924. root_ids: Default::default(),
  3925. dynamic_nodes: __cx.bump().alloc([
  3926. // The dynamic count text node (dynamic node id 0)
  3927. __cx.text_node(format_args!(&quot;High-Five counter: {0}&quot;, count)),
  3928. ]),
  3929. dynamic_attrs: __cx.bump().alloc([
  3930. // The dynamic up high button onclick listener (dynamic attribute id 0)
  3931. dioxus_elements::events::onclick(__cx, move |_| count += 1),
  3932. // The dynamic down low button onclick listener (dynamic attribute id 1)
  3933. dioxus_elements::events::onclick(__cx, move |_| count -= 1),
  3934. ]),
  3935. }
  3936. },
  3937. ),
  3938. )
  3939. }
  3940. </code></pre></pre>
  3941. <p>The rsx macro separates the static parts of the rsx (the template) and the dynamic parts (the dynamic_nodes and dynamic_attributes).</p>
  3942. <p>The static template only contains the parts of the rsx that cannot change at runtime with holes for the dynamic parts:</p>
  3943. <p><a href="https://mermaid.live/edit#pako:eNqdksFuwjAMhl8l8wkkKtFx65njdtm0E0GVSQKJoEmVOgKEeHecUrXStO0wn5Lf9u8vcm6ggjZQwf4UzspiJPH2Ib3g6NLuELG1oiMkp0TsLs9EDu2iUeSCH8tz2HJmy3lRFPrqsXGq9mxeLzcbCU6LZSUGXWRdwnY7tY7Tdoko-Dq1U64fODgiUfzJMeuOe7_ZGq-ny2jNhGQu9DqT8NUK6w72RcL8dxgdzv4PnHLAKf-Fk80HoBUDrfkqeBkTUd8EC2hMbNBpXtYtJySQNQ0PqPioMR4lSH_nOkwUPq9eQUUxmQWkViOZtUN-UwPVHk8dq0Y7CvH9uf3-E9wfrmuk1A"><img src="https://mermaid.ink/img/pako:eNqdksFuwjAMhl8l8wkkKtFx65njdtm0E0GVSQKJoEmVOgKEeHecUrXStO0wn5Lf9u8vcm6ggjZQwf4UzspiJPH2Ib3g6NLuELG1oiMkp0TsLs9EDu2iUeSCH8tz2HJmy3lRFPrqsXGq9mxeLzcbCU6LZSUGXWRdwnY7tY7Tdoko-Dq1U64fODgiUfzJMeuOe7_ZGq-ny2jNhGQu9DqT8NUK6w72RcL8dxgdzv4PnHLAKf-Fk80HoBUDrfkqeBkTUd8EC2hMbNBpXtYtJySQNQ0PqPioMR4lSH_nOkwUPq9eQUUxmQWkViOZtUN-UwPVHk8dq0Y7CvH9uf3-E9wfrmuk1A?type=png" alt="" /></a></p>
  3944. <p>The dynamic_nodes and dynamic_attributes are the parts of the rsx that can change at runtime:</p>
  3945. <p><a href="https://mermaid.live/edit#pako:eNp1UcFOwzAM_RXLVzZpvUbighDiABfgtkxTlnirtSaZUgc0df130hZEEcwny35-79nu0EZHqHDfxA9bmyTw9KIDlGjz7pDMqQZ3DsazhVCQ7dQbwnEiKxwDvN3NqhN4O4C3q_VaIztYKXjkQ7184HcCG3MQSgq6Mes1bjbTPAV3RdqIJN5l-V__2_Fcf5iY68dgG7ZHBT4WD5ftZfIBN7dQ_Tj4w1B9MVTXGZa_GMYdcIGekjfsymW7oaFRavKkUZXUmXTUqENfcCZLfD0Hi0pSpgXmkzNC92zKATyqvWnaUiXHEtPz9KrxY_0nzYOPmA"><img src="https://mermaid.ink/img/pako:eNp1UcFOwzAM_RXLVzZpvUbighDiABfgtkxTlnirtSaZUgc0df130hZEEcwny35-79nu0EZHqHDfxA9bmyTw9KIDlGjz7pDMqQZ3DsazhVCQ7dQbwnEiKxwDvN3NqhN4O4C3q_VaIztYKXjkQ7184HcCG3MQSgq6Mes1bjbTPAV3RdqIJN5l-V__2_Fcf5iY68dgG7ZHBT4WD5ftZfIBN7dQ_Tj4w1B9MVTXGZa_GMYdcIGekjfsymW7oaFRavKkUZXUmXTUqENfcCZLfD0Hi0pSpgXmkzNC92zKATyqvWnaUiXHEtPz9KrxY_0nzYOPmA?type=png" alt="" /></a></p>
  3946. <h2 id="launching-the-app"><a class="header" href="#launching-the-app">Launching the App</a></h2>
  3947. <p>The app is launched by calling the <code>launch</code> function with the root component. Internally, this function will create a new web view using <a href="https://docs.rs/wry/latest/wry/">wry</a> and create a virtual dom with the root component. This guide will not explain the renderer in-depth, but you can read more about it in the <a href="contributing//guide/custom-renderer">custom renderer</a> section.</p>
  3948. <h2 id="the-virtual-dom"><a class="header" href="#the-virtual-dom">The Virtual DOM</a></h2>
  3949. <p>Before we dive into the initial render in the virtual dom, we need to discuss what the virtual dom is. The virtual dom is a representation of the dom that is used to diff the current dom from the new dom. This diff is then used to create a list of mutations that need to be applied to the dom.</p>
  3950. <p>The Virtual Dom roughly looks like this:</p>
  3951. <pre><pre class="playground"><code class="language-rust edition2018">
  3952. <span class="boring">#![allow(unused)]
  3953. </span><span class="boring">fn main() {
  3954. </span>pub struct VirtualDom {
  3955. // All the templates that have been created or set durring hot reloading
  3956. pub(crate) templates: FxHashMap&lt;TemplateId, FxHashMap&lt;usize, Template&lt;'static&gt;&gt;&gt;,
  3957. // A slab of all the scopes that have been created
  3958. pub(crate) scopes: ScopeSlab,
  3959. // All scopes that have been marked as dirty
  3960. pub(crate) dirty_scopes: BTreeSet&lt;DirtyScope&gt;,
  3961. // Every element is actually a dual reference - one to the template and the other to the dynamic node in that template
  3962. pub(crate) elements: Slab&lt;ElementRef&gt;,
  3963. // This receiver is used to receive messages from hooks about what scopes need to be marked as dirty
  3964. pub(crate) rx: futures_channel::mpsc::UnboundedReceiver&lt;SchedulerMsg&gt;,
  3965. // The changes queued up to be sent to the renderer
  3966. pub(crate) mutations: Mutations&lt;'static&gt;,
  3967. }
  3968. <span class="boring">}
  3969. </span></code></pre></pre>
  3970. <blockquote>
  3971. <p>What is a <a href="https://docs.rs/slab/latest/slab/">slab</a>?
  3972. A slab acts like a hashmap with integer keys if you don't care about the value of the keys. It is internally backed by a dense vector which makes it more efficient than a hashmap. When you insert a value into a slab, it returns an integer key that you can use to retrieve the value later.</p>
  3973. </blockquote>
  3974. <blockquote>
  3975. <p>How does Dioxus use slabs?
  3976. Dioxus uses &quot;synchronized slabs&quot; to communicate between the renderer and the VDOM. When an node is created in the Virtual Dom, a ElementId is passed along with the mutation to the renderer to identify the node. These ids are used by the Virtual Dom to reference that nodes in future mutations like setting an attribute on a node or removing a node.
  3977. When the renderer sends an event to the Virtual Dom, it sends the ElementId of the node that the event was triggered on. The Virtual Dom uses this id to find the node in the slab and then run the necessary event handlers.</p>
  3978. </blockquote>
  3979. <p>The virtual dom is a tree of scopes. A new scope is created for every component when it is first rendered and recycled when the component is unmounted.</p>
  3980. <p>Scopes serve three main purposes:</p>
  3981. <ol>
  3982. <li>They store the state of hooks used by the component</li>
  3983. <li>They store the state for the context API</li>
  3984. <li>They store the current and previous VNode that was rendered for diffing</li>
  3985. </ol>
  3986. <h3 id="the-initial-render"><a class="header" href="#the-initial-render">The Initial Render</a></h3>
  3987. <p>The root scope is created and rebuilt:</p>
  3988. <ol>
  3989. <li>The root component is run</li>
  3990. <li>The root component returns a VNode</li>
  3991. <li>Mutations for the VNode are created and added to the mutation list (this may involve creating new child components)</li>
  3992. <li>The VNode is stored in the root scope</li>
  3993. </ol>
  3994. <p>After the root scope is built, the mutations are sent to the renderer to be applied to the dom.</p>
  3995. <p>After the initial render, the root scope looks like this:</p>
  3996. <p><a href="https://mermaid.live/edit#pako:eNqtVE1P4zAQ_SuzPrWikRpWXCLtBRDisItWsOxhCaqM7RKricdyJrQV8N93QtvQNCkfEnOynydv3nxkHoVCbUQipjnOVSYDwc_L1AFbWd3dB-kzuEQkuFLoDUwDFkCZAek9nGDh0RlHK__atA1GkUUHf45f0YbppAqB_aOzIAvz-t7-chN_Y-1bw1WSJKsglIu2w9tktWXxIIuHURT5XCqTYa5NmDguw2R8c5MKq2GcgF46WTB_jafi9rZL0yi5q4jQTSrf9altO4okCn1Ratwyz55Qxuku2ITlTMgs6HCQimsPmb3PvqVi-L5gjXP3QcnxWnL8JZLrwGvR31n0KV-Bx6-r-oVkT_-3G1S-NQLbk9i8rj7udP2cixed2QcDCitHJiQw7ub3EVlNecrPjudG2-6soFO5VbMECmR9T5OnlUY4-AFxfw9aTFst3McU9TK1Otm6NEn_DubBYlX2_dglLXOz48FgwJmJ5lZTlhz6xWgNaFnyDgpymcARHO0W2a9J_l5w2wYXvHuGPcqaQ-rESBQmFNJq3nCPNZoK3l4sUSR81DLMUpG6Z_aTFeHV0imRUKjMSFReSzKnVnKGhUimMi8ZNdoShl-rlfmyOUfCS_cPcePz_B_Wl4pc"><img src="https://mermaid.ink/img/pako:eNqtVE1P4zAQ_SuzPrWikRpWXCLtBRDisItWsOxhCaqM7RKricdyJrQV8N93QtvQNCkfEnOynydv3nxkHoVCbUQipjnOVSYDwc_L1AFbWd3dB-kzuEQkuFLoDUwDFkCZAek9nGDh0RlHK__atA1GkUUHf45f0YbppAqB_aOzIAvz-t7-chN_Y-1bw1WSJKsglIu2w9tktWXxIIuHURT5XCqTYa5NmDguw2R8c5MKq2GcgF46WTB_jafi9rZL0yi5q4jQTSrf9altO4okCn1Ratwyz55Qxuku2ITlTMgs6HCQimsPmb3PvqVi-L5gjXP3QcnxWnL8JZLrwGvR31n0KV-Bx6-r-oVkT_-3G1S-NQLbk9i8rj7udP2cixed2QcDCitHJiQw7ub3EVlNecrPjudG2-6soFO5VbMECmR9T5OnlUY4-AFxfw9aTFst3McU9TK1Otm6NEn_DubBYlX2_dglLXOz48FgwJmJ5lZTlhz6xWgNaFnyDgpymcARHO0W2a9J_l5w2wYXvHuGPcqaQ-rESBQmFNJq3nCPNZoK3l4sUSR81DLMUpG6Z_aTFeHV0imRUKjMSFReSzKnVnKGhUimMi8ZNdoShl-rlfmyOUfCS_cPcePz_B_Wl4pc?type=png" alt="" /></a></p>
  3997. <h3 id="waiting-for-events"><a class="header" href="#waiting-for-events">Waiting for Events</a></h3>
  3998. <p>The Virtual Dom will only ever rerender a scope if it is marked as dirty. Each hook is responsible for marking the scope as dirty if the state has changed. Hooks can mark a scope as dirty by sending a message to the Virtual Dom's channel.</p>
  3999. <p>There are generally two ways a scope is marked as dirty:</p>
  4000. <ol>
  4001. <li>The renderer triggers an event: This causes an event listener to be called if needed which may mark a component as dirty</li>
  4002. <li>The renderer calls wait for work: This polls futures which may mark a component as dirty</li>
  4003. </ol>
  4004. <p>Once at least one scope is marked as dirty, the renderer can call <code>render_with_deadline</code> to diff the dirty scopes.</p>
  4005. <h3 id="diffing-scopes"><a class="header" href="#diffing-scopes">Diffing Scopes</a></h3>
  4006. <p>If the user clicked the &quot;up high&quot; button, the root scope would be marked as dirty by the use_state hook. Once the desktop renderer calls <code>render_with_deadline</code>, the root scope would be diffed.</p>
  4007. <p>To start the diffing process, the component is run. After the root component is run it will look like this:</p>
  4008. <p><a href="https://mermaid.live/edit#pako:eNrFVlFP2zAQ_iuen0BrpCaIl0i8AEJ72KQJtpcRFBnbJVYTn-U4tBXw33dpG5M2CetoBfdkny_ffb67fPIT5SAkjekkhxnPmHXk-3WiCVpZ3T9YZjJyDeDIDQcjycRCQVwmCTOGXEBhQEvtVvG1CWUldwo0-XX-6vVIF5W1GB9cWVbI1_PNL5v8jW3uPFbpmFOc2HK-GfA2WG1ZeJSFx0EQmJxxmUEupE01liEd394mVAkyjolYaFYgfu1P6N1dF8Yzua-cA51WphtTWzsLc872Zan9CnEGUkktuk6fFm_i5NxFRwn9bUimHrIvCT3-N2EBM70j5XBNOTwI5TrxmvQJkr7ELcHx67Jeggz0v92g8q0RaE-iP1193On6NyxecKUeJeFQaSdtTMLu_Xah5ctT_u94Nty2ZwU0zxWfxqQA5PecPq84kq9nfRw7SK0WDiEFZ4O37d34S_-08lFBVfb92KVb5HIrAp0WpjKYKeGyODLz0dohWIkaZNkiJqfkdLvIH6oRaTSoEmm0n06k0a5K0ZdpL61Io0Yt0nfpxc7UQ0_9cJrhyZ8syX-6brS706Mc489Vjja7fbWj3cxDqIdfJJqOaCFtwZTAV8hT7U0ovjBQRmiMS8HsNKGJfsE4Vjm4WWhOY2crOaKVEczJS8WwgAWNJywv0SuFcmB_rJ41y9fNiBqm_wA0MS9_AUuAiy0"><img src="https://mermaid.ink/img/pako:eNrFVlFP2zAQ_iuen0BrpCaIl0i8AEJ72KQJtpcRFBnbJVYTn-U4tBXw33dpG5M2CetoBfdkny_ffb67fPIT5SAkjekkhxnPmHXk-3WiCVpZ3T9YZjJyDeDIDQcjycRCQVwmCTOGXEBhQEvtVvG1CWUldwo0-XX-6vVIF5W1GB9cWVbI1_PNL5v8jW3uPFbpmFOc2HK-GfA2WG1ZeJSFx0EQmJxxmUEupE01liEd394mVAkyjolYaFYgfu1P6N1dF8Yzua-cA51WphtTWzsLc872Zan9CnEGUkktuk6fFm_i5NxFRwn9bUimHrIvCT3-N2EBM70j5XBNOTwI5TrxmvQJkr7ELcHx67Jeggz0v92g8q0RaE-iP1193On6NyxecKUeJeFQaSdtTMLu_Xah5ctT_u94Nty2ZwU0zxWfxqQA5PecPq84kq9nfRw7SK0WDiEFZ4O37d34S_-08lFBVfb92KVb5HIrAp0WpjKYKeGyODLz0dohWIkaZNkiJqfkdLvIH6oRaTSoEmm0n06k0a5K0ZdpL61Io0Yt0nfpxc7UQ0_9cJrhyZ8syX-6brS706Mc489Vjja7fbWj3cxDqIdfJJqOaCFtwZTAV8hT7U0ovjBQRmiMS8HsNKGJfsE4Vjm4WWhOY2crOaKVEczJS8WwgAWNJywv0SuFcmB_rJ41y9fNiBqm_wA0MS9_AUuAiy0?type=png" alt="" /></a></p>
  4009. <p>Next, the Virtual Dom will compare the new VNode with the previous VNode and only update the parts of the tree that have changed.</p>
  4010. <p>When a component is re-rendered, the Virtual Dom will compare the new VNode with the previous VNode and only update the parts of the tree that have changed.</p>
  4011. <p>The diffing algorithm goes through the list of dynamic attributes and nodes and compares them to the previous VNode. If the attribute or node has changed, a mutation that describes the change is added to the mutation list.</p>
  4012. <p>Here is what the diffing algorithm looks like for the root scope (red lines indicate that a mutation was generated, and green lines indicate that no mutation was generated)</p>
  4013. <p><a href="https://mermaid.live/edit#pako:eNrFlFFPwjAQx7_KpT7Kko2Elya8qCE-aGLAJ5khpe1Yw9Zbug4k4He3OJjbGPig0T5t17tf_nf777aEo5CEkijBNY-ZsfAwDjW4kxfzhWFZDGNECxOOmYTIYAo2lsCyDG4xzVBLbcv8_RHKSG4V6orSIN0Wxrh8b2RYKr_uTyubd1W92GiWKg7aac6bOU3G803HbVk82xfP_Ok0JEqAT-FeLWJvpFYSOBbaSkMhCMnra5MgtfhWFrPWqHlhL2urT6atbU-oa0PNE8WXFFJ0-nazXakRroddGk9IwYEUnCd5w7Pddr5UTT8ZuVJY5F0fM7ebRLYyXNDgUnprJWxM-9lb7xAQLHe-M2xDYQCD9pD_2hez_kVn-P_rjLq6n3qjYv2iO5qz9DyvPdyv1ETp5eTTJ_7BGvQq8v1TVtl5jXUcRRcrqFh-dI4VtFlBN6t_ynLNkh5JpUmZEm5rbvfhkLiN6H4BQt2jYGYZklC_uzxWWJxsNCfUmkL2SJEJZuWdYs4cKaERS3IXlUJZNI_lGv7cxj2SMf2CeMx5_wBcbK19"><img src="https://mermaid.ink/img/pako:eNrFlFFPwjAQx7_KpT7Kko2Elya8qCE-aGLAJ5khpe1Yw9Zbug4k4He3OJjbGPig0T5t17tf_nf777aEo5CEkijBNY-ZsfAwDjW4kxfzhWFZDGNECxOOmYTIYAo2lsCyDG4xzVBLbcv8_RHKSG4V6orSIN0Wxrh8b2RYKr_uTyubd1W92GiWKg7aac6bOU3G803HbVk82xfP_Ok0JEqAT-FeLWJvpFYSOBbaSkMhCMnra5MgtfhWFrPWqHlhL2urT6atbU-oa0PNE8WXFFJ0-nazXakRroddGk9IwYEUnCd5w7Pddr5UTT8ZuVJY5F0fM7ebRLYyXNDgUnprJWxM-9lb7xAQLHe-M2xDYQCD9pD_2hez_kVn-P_rjLq6n3qjYv2iO5qz9DyvPdyv1ETp5eTTJ_7BGvQq8v1TVtl5jXUcRRcrqFh-dI4VtFlBN6t_ynLNkh5JpUmZEm5rbvfhkLiN6H4BQt2jYGYZklC_uzxWWJxsNCfUmkL2SJEJZuWdYs4cKaERS3IXlUJZNI_lGv7cxj2SMf2CeMx5_wBcbK19?type=png" alt="" /></a></p>
  4014. <h2 id="conclusion-2"><a class="header" href="#conclusion-2">Conclusion</a></h2>
  4015. <p>This is only a brief overview of how the Virtual Dom works. There are several aspects not yet covered in this guide including how the Virtual Dom handles async-components, keyed diffing, and how it uses <a href="https://github.com/fitzgen/bumpalo">bump allocation</a> to efficiently allocate VNodes. If need more information about the Virtual Dom, you can read the code of the <a href="https://github.com/DioxusLabs/dioxus/tree/master/packages/core">core</a> crate or reach out to us on <a href="https://discord.gg/XgGxMSkvUM">Discord</a>.</p>
  4016. <div style="break-before: page; page-break-before: always;"></div><h1 id="overall-goals"><a class="header" href="#overall-goals">Overall Goals</a></h1>
  4017. <p>This document outlines some of the overall goals for Dioxus. These goals are not set in stone, but they represent general guidelines for the project.</p>
  4018. <p>The goal of Dioxus is to make it easy to build <strong>cross-platform applications that scale</strong>.</p>
  4019. <h2 id="cross-platform"><a class="header" href="#cross-platform">Cross-Platform</a></h2>
  4020. <p>Dioxus is designed to be cross-platform by default. This means that it should be easy to build applications that run on the web, desktop, and mobile. However, Dioxus should also be flexible enough to allow users to opt into platform-specific features when needed. The <code>use_eval</code> is one example of this. By default, Dioxus does not assume that the platform supports JavaScript, but it does provide a hook that allows users to opt into JavaScript when needed.</p>
  4021. <h2 id="performance"><a class="header" href="#performance">Performance</a></h2>
  4022. <p>As Dioxus applications grow, they should remain relatively performant without the need for manual optimizations. There will be cases where manual optimizations are needed, but Dioxus should try to make these cases as rare as possible.</p>
  4023. <p>One of the benefits of the core architecture of Dioxus is that it delivers reasonable performance even when components are rerendered often. It is based on a Virtual Dom which performs diffing which should prevent unnecessary re-renders even when large parts of the component tree are rerun. On top of this, Dioxus groups static parts of the RSX tree together to skip diffing them entirely.</p>
  4024. <h2 id="type-safety"><a class="header" href="#type-safety">Type Safety</a></h2>
  4025. <p>As teams grow, the Type safety of Rust is a huge advantage. Dioxus should leverage this advantage to make it easy to build applications with large teams.</p>
  4026. <p>To take full advantage of Rust's type system, Dioxus should try to avoid exposing public <code>Any</code> types and string-ly typed APIs where possible.</p>
  4027. <h2 id="developer-experience"><a class="header" href="#developer-experience">Developer Experience</a></h2>
  4028. <p>Dioxus should be easy to learn and ergonomic to use.</p>
  4029. <ul>
  4030. <li>
  4031. <p>The API of Dioxus attempts to remain close to React's API where possible. This makes it easier for people to learn Dioxus if they already know React</p>
  4032. </li>
  4033. <li>
  4034. <p>We can avoid the tradeoff between simplicity and flexibility by providing multiple layers of API: One for the very common use case, one for low-level control</p>
  4035. <ul>
  4036. <li>Hooks: the hooks crate has the most common use cases, but <code>cx.hook</code> provides a way to access the underlying persistent reference if needed.</li>
  4037. <li>The builder pattern in platform Configs: The builder pattern is used to default to the most common use case, but users can change the defaults if needed.</li>
  4038. </ul>
  4039. </li>
  4040. <li>
  4041. <p>Documentation:</p>
  4042. <ul>
  4043. <li>All public APIs should have rust documentation</li>
  4044. <li>Examples should be provided for all public features. These examples both serve as documentation and testing. They are checked by CI to ensure that they continue to compile</li>
  4045. <li>The most common workflows should be documented in the guide</li>
  4046. </ul>
  4047. </li>
  4048. </ul>
  4049. <div style="break-before: page; page-break-before: always;"></div><h1 id="roadmap--feature-set"><a class="header" href="#roadmap--feature-set">Roadmap &amp; Feature-set</a></h1>
  4050. <p>This feature set and roadmap can help you decide if what Dioxus can do today works for you.</p>
  4051. <p>If a feature that you need doesn't exist or you want to contribute to projects on the roadmap, feel free to get involved by <a href="https://discord.gg/XgGxMSkvUM">joining the discord</a>.</p>
  4052. <p>Generally, here's the status of each platform:</p>
  4053. <ul>
  4054. <li>
  4055. <p><strong>Web</strong>: Dioxus is a great choice for pure web-apps – especially for CRUD/complex apps. However, it does lack the ecosystem of React, so you might be missing a component library or some useful hook.</p>
  4056. </li>
  4057. <li>
  4058. <p><strong>SSR</strong>: Dioxus is a great choice for pre-rendering, hydration, and rendering HTML on a web endpoint. Be warned – the VirtualDom is not (currently) <code>Send + Sync</code>.</p>
  4059. </li>
  4060. <li>
  4061. <p><strong>Desktop</strong>: You can build very competent single-window desktop apps right now. However, multi-window apps require support from Dioxus core and are not ready.</p>
  4062. </li>
  4063. <li>
  4064. <p><strong>Mobile</strong>: Mobile support is very young. You'll be figuring things out as you go and there are not many support crates for peripherals.</p>
  4065. </li>
  4066. <li>
  4067. <p><strong>LiveView</strong>: LiveView support is very young. You'll be figuring things out as you go. Thankfully, none of it is too hard and any work can be upstreamed into Dioxus.</p>
  4068. </li>
  4069. </ul>
  4070. <h2 id="features-1"><a class="header" href="#features-1">Features</a></h2>
  4071. <hr />
  4072. <table><thead><tr><th>Feature</th><th>Status</th><th>Description</th></tr></thead><tbody>
  4073. <tr><td>Conditional Rendering</td><td>✅</td><td>if/then to hide/show component</td></tr>
  4074. <tr><td>Map, Iterator</td><td>✅</td><td>map/filter/reduce to produce rsx!</td></tr>
  4075. <tr><td>Keyed Components</td><td>✅</td><td>advanced diffing with keys</td></tr>
  4076. <tr><td>Web</td><td>✅</td><td>renderer for web browser</td></tr>
  4077. <tr><td>Desktop (webview)</td><td>✅</td><td>renderer for desktop</td></tr>
  4078. <tr><td>Shared State (Context)</td><td>✅</td><td>share state through the tree</td></tr>
  4079. <tr><td>Hooks</td><td>✅</td><td>memory cells in components</td></tr>
  4080. <tr><td>SSR</td><td>✅</td><td>render directly to string</td></tr>
  4081. <tr><td>Component Children</td><td>✅</td><td>cx.children() as a list of nodes</td></tr>
  4082. <tr><td>Headless components</td><td>✅</td><td>components that don't return real elements</td></tr>
  4083. <tr><td>Fragments</td><td>✅</td><td>multiple elements without a real root</td></tr>
  4084. <tr><td>Manual Props</td><td>✅</td><td>Manually pass in props with spread syntax</td></tr>
  4085. <tr><td>Controlled Inputs</td><td>✅</td><td>stateful wrappers around inputs</td></tr>
  4086. <tr><td>CSS/Inline Styles</td><td>✅</td><td>syntax for inline styles/attribute groups</td></tr>
  4087. <tr><td>Custom elements</td><td>✅</td><td>Define new element primitives</td></tr>
  4088. <tr><td>Suspense</td><td>✅</td><td>schedule future render from future/promise</td></tr>
  4089. <tr><td>Integrated error handling</td><td>✅</td><td>Gracefully handle errors with ? syntax</td></tr>
  4090. <tr><td>NodeRef</td><td>✅</td><td>gain direct access to nodes</td></tr>
  4091. <tr><td>Re-hydration</td><td>✅</td><td>Pre-render to HTML to speed up first contentful paint</td></tr>
  4092. <tr><td>Jank-Free Rendering</td><td>✅</td><td>Large diffs are segmented across frames for silky-smooth transitions</td></tr>
  4093. <tr><td>Effects</td><td>✅</td><td>Run effects after a component has been committed to render</td></tr>
  4094. <tr><td>Portals</td><td>🛠</td><td>Render nodes outside of the traditional tree structure</td></tr>
  4095. <tr><td>Cooperative Scheduling</td><td>🛠</td><td>Prioritize important events over non-important events</td></tr>
  4096. <tr><td>Server Components</td><td>🛠</td><td>Hybrid components for SPA and Server</td></tr>
  4097. <tr><td>Bundle Splitting</td><td>👀</td><td>Efficiently and asynchronously load the app</td></tr>
  4098. <tr><td>Lazy Components</td><td>👀</td><td>Dynamically load the new components as the page is loaded</td></tr>
  4099. <tr><td>1st class global state</td><td>✅</td><td>redux/recoil/mobx on top of context</td></tr>
  4100. <tr><td>Runs natively</td><td>✅</td><td>runs as a portable binary w/o a runtime (Node)</td></tr>
  4101. <tr><td>Subtree Memoization</td><td>✅</td><td>skip diffing static element subtrees</td></tr>
  4102. <tr><td>High-efficiency templates</td><td>✅</td><td>rsx! calls are translated to templates on the DOM's side</td></tr>
  4103. <tr><td>Compile-time correct</td><td>✅</td><td>Throw errors on invalid template layouts</td></tr>
  4104. <tr><td>Heuristic Engine</td><td>✅</td><td>track component memory usage to minimize future allocations</td></tr>
  4105. <tr><td>Fine-grained reactivity</td><td>👀</td><td>Skip diffing for fine-grain updates</td></tr>
  4106. </tbody></table>
  4107. <ul>
  4108. <li>✅ = implemented and working</li>
  4109. <li>🛠 = actively being worked on</li>
  4110. <li>👀 = not yet implemented or being worked on</li>
  4111. </ul>
  4112. <h2 id="roadmap"><a class="header" href="#roadmap">Roadmap</a></h2>
  4113. <p>These Features are planned for the future of Dioxus:</p>
  4114. <h3 id="core"><a class="header" href="#core">Core</a></h3>
  4115. <ul>
  4116. <li><input disabled="" type="checkbox" checked=""/>
  4117. Release of Dioxus Core</li>
  4118. <li><input disabled="" type="checkbox" checked=""/>
  4119. Upgrade documentation to include more theory and be more comprehensive</li>
  4120. <li><input disabled="" type="checkbox" checked=""/>
  4121. Support for HTML-side templates for lightning-fast dom manipulation</li>
  4122. <li><input disabled="" type="checkbox"/>
  4123. Support for multiple renderers for same virtualdom (subtrees)</li>
  4124. <li><input disabled="" type="checkbox"/>
  4125. Support for ThreadSafe (Send + Sync)</li>
  4126. <li><input disabled="" type="checkbox"/>
  4127. Support for Portals</li>
  4128. </ul>
  4129. <h3 id="ssr"><a class="header" href="#ssr">SSR</a></h3>
  4130. <ul>
  4131. <li><input disabled="" type="checkbox" checked=""/>
  4132. SSR Support + Hydration</li>
  4133. <li><input disabled="" type="checkbox"/>
  4134. Integrated suspense support for SSR</li>
  4135. </ul>
  4136. <h3 id="desktop"><a class="header" href="#desktop">Desktop</a></h3>
  4137. <ul>
  4138. <li><input disabled="" type="checkbox"/>
  4139. Declarative window management</li>
  4140. <li><input disabled="" type="checkbox"/>
  4141. Templates for building/bundling</li>
  4142. <li><input disabled="" type="checkbox"/>
  4143. Access to Canvas/WebGL context natively</li>
  4144. </ul>
  4145. <h3 id="mobile"><a class="header" href="#mobile">Mobile</a></h3>
  4146. <ul>
  4147. <li><input disabled="" type="checkbox"/>
  4148. Mobile standard library
  4149. <ul>
  4150. <li><input disabled="" type="checkbox"/>
  4151. GPS</li>
  4152. <li><input disabled="" type="checkbox"/>
  4153. Camera</li>
  4154. <li><input disabled="" type="checkbox"/>
  4155. filesystem</li>
  4156. <li><input disabled="" type="checkbox"/>
  4157. Biometrics</li>
  4158. <li><input disabled="" type="checkbox"/>
  4159. WiFi</li>
  4160. <li><input disabled="" type="checkbox"/>
  4161. Bluetooth</li>
  4162. <li><input disabled="" type="checkbox"/>
  4163. Notifications</li>
  4164. <li><input disabled="" type="checkbox"/>
  4165. Clipboard</li>
  4166. </ul>
  4167. </li>
  4168. <li><input disabled="" type="checkbox"/>
  4169. Animations</li>
  4170. </ul>
  4171. <h3 id="bundling-cli"><a class="header" href="#bundling-cli">Bundling (CLI)</a></h3>
  4172. <ul>
  4173. <li><input disabled="" type="checkbox" checked=""/>
  4174. Translation from HTML into RSX</li>
  4175. <li><input disabled="" type="checkbox" checked=""/>
  4176. Dev server</li>
  4177. <li><input disabled="" type="checkbox" checked=""/>
  4178. Live reload</li>
  4179. <li><input disabled="" type="checkbox" checked=""/>
  4180. Translation from JSX into RSX</li>
  4181. <li><input disabled="" type="checkbox"/>
  4182. Hot module replacement</li>
  4183. <li><input disabled="" type="checkbox"/>
  4184. Code splitting</li>
  4185. <li><input disabled="" type="checkbox"/>
  4186. Asset macros</li>
  4187. <li><input disabled="" type="checkbox"/>
  4188. Css pipeline</li>
  4189. <li><input disabled="" type="checkbox"/>
  4190. Image pipeline</li>
  4191. </ul>
  4192. <h3 id="essential-hooks"><a class="header" href="#essential-hooks">Essential hooks</a></h3>
  4193. <ul>
  4194. <li><input disabled="" type="checkbox" checked=""/>
  4195. Router</li>
  4196. <li><input disabled="" type="checkbox" checked=""/>
  4197. Global state management</li>
  4198. <li><input disabled="" type="checkbox"/>
  4199. Resize observer</li>
  4200. </ul>
  4201. <h2 id="work-in-progress"><a class="header" href="#work-in-progress">Work in Progress</a></h2>
  4202. <h3 id="build-tool"><a class="header" href="#build-tool">Build Tool</a></h3>
  4203. <p>We are currently working on our own build tool called <a href="https://github.com/DioxusLabs/cli">Dioxus CLI</a> which will support:</p>
  4204. <ul>
  4205. <li>an interactive TUI</li>
  4206. <li>on-the-fly reconfiguration</li>
  4207. <li>hot CSS reloading</li>
  4208. <li>two-way data binding between browser and source code</li>
  4209. <li>an interpreter for <code>rsx!</code></li>
  4210. <li>ability to publish to github/netlify/vercel</li>
  4211. <li>bundling for iOS/Desktop/etc</li>
  4212. </ul>
  4213. <h3 id="server-component-support"><a class="header" href="#server-component-support">Server Component Support</a></h3>
  4214. <p>While not currently fully implemented, the expectation is that LiveView apps can be a hybrid between Wasm and server-rendered where only portions of a page are &quot;live&quot; and the rest of the page is either server-rendered, statically generated, or handled by the host SPA.</p>
  4215. <h3 id="native-rendering"><a class="header" href="#native-rendering">Native rendering</a></h3>
  4216. <p>We are currently working on a native renderer for Dioxus using WGPU called <a href="https://github.com/DioxusLabs/blitz/">Blitz</a>. This will allow you to build apps that are rendered natively for iOS, Android, and Desktop.</p>
  4217. </main>
  4218. <nav class="nav-wrapper" aria-label="Page navigation">
  4219. <!-- Mobile navigation buttons -->
  4220. <div style="clear: both"></div>
  4221. </nav>
  4222. </div>
  4223. </div>
  4224. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  4225. </nav>
  4226. </div>
  4227. <script type="text/javascript">
  4228. window.playground_line_numbers = true;
  4229. </script>
  4230. <script type="text/javascript">
  4231. window.playground_copyable = true;
  4232. </script>
  4233. <script src="ace.js" type="text/javascript" charset="utf-8"></script>
  4234. <script src="editor.js" type="text/javascript" charset="utf-8"></script>
  4235. <script src="mode-rust.js" type="text/javascript" charset="utf-8"></script>
  4236. <script src="theme-dawn.js" type="text/javascript" charset="utf-8"></script>
  4237. <script src="theme-tomorrow_night.js" type="text/javascript" charset="utf-8"></script>
  4238. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  4239. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  4240. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  4241. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  4242. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  4243. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  4244. <!-- Custom JS scripts -->
  4245. <script type="text/javascript">
  4246. window.addEventListener('load', function() {
  4247. MathJax.Hub.Register.StartupHook('End', function() {
  4248. window.setTimeout(window.print, 100);
  4249. });
  4250. });
  4251. </script>
  4252. </body>
  4253. </html>