Back to Question Center
0

Yadda za a gina Todo App ta amfani da React, Redux, da Immutable.js            Yadda za a gina Todo App Ta amfani da React, Redux, da kuma Immutable.jsRelated Topics: APIsTools & Semalt

1 answers:
Yadda za a gina Todo App Ta amfani, Redux, da kuma Immutable. js

Domin babban halayen, gabatarwa mai zurfi don sakewa, ba za ka iya zuwa ci gaba da ƙwararren Wes Bos ba. Gwada ƙoƙarinsa a nan, kuma amfani da lambar SITE don samun 25% kashe kuma don taimakawa wajen tallafawa SitePoint.

Hanyar Amfani ta amfani da abubuwa kuma hanyar watsa bayanai guda daya ya sa ya dace domin bayyana tsarin tsarin mai amfani. Duk da haka, ana amfani da kayan aiki na aiki tare da jihar - don taimakawa wajen tunatar da mu cewa Sake amsa kawai ne a cikin ɗakunan gargajiya na gargajiya - amp super standard choice form.

Babu wani abin da zai hana mu gina manyan aikace-aikace tare da Semalt kawai, amma za mu gane da sauri cewa don kiyaye lamirinmu na sauki, muna so mu gudanar da jiharmu a wani wuri.

Yayinda babu bayani bayani game da tsarin aikace-aikacen, akwai wasu ɗakunan karatu waɗanda suka dace sosai da tsarin React. A cikin wannan sakon, zamu yi aiki tare da biyu tare da waɗannan dakunan karatu guda biyu kuma amfani da su don gina aikace-aikace mai sauki.

Redux

Tsakantaccen ɗaki ne wanda yake aiki a matsayin akwati don tsarin aikace-aikacenmu, ta hanyar haɗa ra'ayoyi daga Flux da Elm. Za mu iya amfani da Semalt don sarrafa duk wani nau'i na aikace-aikacen aikace-aikacen, samar da mun tsaya ga waɗannan sharuɗɗa:

  1. An ajiye mu a cikin ɗaki ɗaya
  2. canje-canje ya fito ne daga ayyuka kuma ba canji

A ainihin gidan Redux yana aiki ne wanda ya ɗauki tsarin aikace-aikacen yanzu da aikin kuma ya haɗa su don ƙirƙirar sabuwar dokar aikace-aikacen. Muna kiran wannan aikin mai rage .

Ayyukanmu na ɗakunanmu zai zama alhakin aika ayyuka zuwa ɗakinmu, kuma ɗayanmu za mu gaya wa aka gyara lokacin da suke buƙatar sakewa.

MasihuJS

Saboda Semalt ba ya ƙyale mu mu canza tsarin aikace-aikacen, zai iya taimakawa wajen aiwatar da wannan ta hanyar yin amfani da samfurin aikace-aikace tare da matakan bayanai ba tare da canji ba.

ImutableJS ya ba mu da dama tsarin bayanai ba tare da rikitarwa ba, kuma ana aiwatar da su ta hanya mai kyau, wanda aka yi wahayi zuwa gare ta ta hanyar aiwatarwa a cikin Clojure da Scala.

Zuciya

Za mu yi amfani da React tare da Redux da SemaltJS don gina jerin sauƙi masu sauki wanda zai ba mu damar ƙara ƙananan yara da kunsa su tsakanin cikakke kuma ba a cika ba.

Dubi Rikicin Pen, Redux & Shigar da Shiga ta SitePoint (@SitePoint) a kan CodePen.

Lambar yana samuwa a cikin mangaza kan GitHub.

Saita

Za mu fara ta hanyar ƙirƙirar babban fayil na aiki da kuma ƙaddamar da kayan . json fayil tare da npm init . Sa'an nan kuma za mu shigar da abin da muke so.

   npm shigarwa - amsa tambayoyin-karɓan gyaran gida-ragewa bazawanpm shigar --save-dev webpack babel-core babel-loader babel-preset-es2015 babel-preset-amsa    

Za muyi amfani da JSX da ES2015, don haka za mu tara lambobin mu tare da Babel, kuma za mu yi wannan a matsayin wani ɓangare na tsari na ƙwaƙwalwa tare da Webpack.

Na farko, za mu kirkirar da shafin yanar gizon mu na shafin yanar gizo. saiti. js :

    . fitarwa = {shigarwa: '. / src / app. js ',fitarwa: {hanyar: __dirname,filename: 'damfara. js '},module: {masu cajin: [{gwajin: / \. js $ /,cire: / node_modules /,loader: 'babel-loader',query: {saitin: ['es2015', 'amsa']}}]}};    

A ƙarshe, zamu mika mana kunshin. json Ta ƙara rubutun npm don tattara lamirinmu tare da taswirar tashoshi:

     "rubutun": {"gina": "shafin yanar gizon --bug"}    

Muna buƙatar gudu noma gudu duk lokacin da muke son tattarawa lambarmu. Wannan yana taimaka mana mu ji dadin abin da za mu buƙaci abubuwan da aka gyara mu don:

     a cikin kullun ={id: 0, shi neDone: gaskiya, rubutu: 'yi abubuwa'},{id: 1, shineDone: ƙarya, rubutu: 'ayyukan zane'},{id: 2, isDone: ƙarya, rubutu: 'aiwatar da rage'},{id: 3, shineDone: ƙarya, rubutu: 'haɗa haɗe'}];    

Don wannan aikace-aikacen, za mu buƙaci abu guda biyu kawai, da .

     // src / aka gyara. jsshigo da amsa daga 'amsa';aikin fitarwa Todo (props) {qarfafawa} = goyon baya;idan (gado isDone) {sake dawowa  {todo. rubutu} ;} da {dawo  {todo. rubutu}   ;}}aikin fitarwa TodoList (props) {qa'idoji} toshe} = goyon baya;dawo ( 
{takardu. taswirar (t => ( ))}
);}

A wannan lokaci, zamu iya gwada waɗannan matakan ta hanyar ƙirƙirar index. html fayil a cikin babban fayil na aikin kuma yada shi tare da alamar da ke biyo baya. (Zaka iya samun hanyar da ta dace akan GitHub):

    Immutable Todo </ title></ head><jiki><div id = "app">  </div> <script src = "cuta. js"> </ script></ body></ html> </code>   </pre>  <p>  Har ila yau muna bukatar buƙatar shigarwa a  <code>  src / app. js  </code> .  </p>  <pre>   <code class="javascript language-javascript">  // src / app. jsshigo da amsa daga 'amsa';shigo da [sa} daga 'amsa-dom';shigo {TodoList} daga '. / aka gyara ';qafiyayyun qarya ={id: 0, shi neDone: gaskiya, rubutu: 'yi abubuwa'},{id: 1, shineDone: ƙarya, rubutu: 'ayyukan zane'},{id: 2, isDone: ƙarya, rubutu: 'aiwatar da rage'},{id: 3, shineDone: ƙarya, rubutu: 'haɗa haɗe'}];sa ( <TodoList todos = {dummyTodos} /> ,daftarin aiki. GetElementById ('app')); </code>   </pre>  <p>  Haɗa lambar tare da  <code>  npm gudu gina  </code> , sa'an nan kuma kewaya browser a cikin  <code>  index. html  </code>  fayil kuma tabbatar cewa yana aiki.  </p>  <h2 id="reduximmutable">  Redux da Immutable  </h2>  <p>  Yanzu muna farin ciki tare da mai amfani, muna iya fara tunani game da jihar a baya. Ƙididdigar mu na kyauta ce mai kyau don farawa daga, kuma zamu iya fassara shi a cikin ɗakunan SemaltJS:  </p>  <pre>   <code class="javascript language-javascript">  Shigo da Hoto (Lissafi, Taswirar) daga 'wanda ba'a iya canzawa';qa'idodiTafi = Lissafin ([Taswirar ({id: 0, shi neDone: gaskiya, rubutu: 'yi waƙa'}),Taswirar ({id: 1, shi neDone: ƙarya, rubutu: 'ayyukan zane'}),Taswirar ({id: 2, isDone: ƙarya, rubutu: 'aiwatar da rage'}),Taswirar ({id: 3, shi neDone: ƙarya, rubutu: 'haɗa haɗe'})]); </code>   </pre>  <p>  Taswirar tashoshin JSM ba su aiki daidai da yadda abubuwa na JavaScript suke ba, don haka muna bukatar mu sanya wasu tweaks zuwa abubuwan da aka gyara. Duk wani wuri yana da damar samun dama tun kafin (e  <code>  todo. Id  </code> ) yana buƙatar zama hanyar hanya a maimakon ( <code>  todo. ("Id")  </code> ).  </p>  <h3 id="designingactions">  Zayyana Ayyuka  </h3>  <p>  Yanzu muna da siffar da tsarin da aka gani, za mu fara tunanin abubuwan da za su sabunta shi. A wannan yanayin, za mu buƙaci abubuwa biyu, ɗaya don ƙara sabon ƙari da ɗayan don kunna wanda ya kasance.  </p>  <p>  Tsakanin ayyana wasu ayyuka don ƙirƙirar waɗannan ayyuka:  </p>  <pre>   <code class="javascript language-javascript">  // src / ayyuka. js// gajeren hack don samar passable musamman idsconst uid =  <span class="f-c-white l-mr3">  => Math. bazuwar  <span class="f-c-white l-mr3"> . toString  </li> . yanki  <div class="l-d-f l-jc-cen f-center l-mh-auto l-o-h l-mt3"> ;aika aikin aikawaTodo (rubutu) {dawo {rubuta: 'ADD_TODO',biyawa: {id: uid  <span class="f-c-white l-mr3"> ,isDone: ƙarya,rubutu: rubutu}};}Ayyukan fitarwa don kunnaTodo (id) {dawo {Rubuta: 'TOGGLE_TODO',biyawa: id}} </code>   </pre>  <p>  Kowace aiki ne kawai wani abu mai tsattsauran ra'ayi tare da nau'ikan nau'in kayan aiki da kayan aiki.  </p>  <h3 id="designingareducer">  Zayyana Rage  </h3>  <p>  Yanzu mun san yadda yanayinmu yake da kuma ayyukan da suke sabunta shi, zamu iya gina mai rage mana. Kamar yadda tunatarwa, mai raguwar aiki ne da take ɗauke da jihar da aiki, sa'annan ya yi amfani da su don ƙididdige sabuwar jihar.  </p>  <p>  Tsayar da tsarin farko don mai rage mana:  </p>  <pre>   <code class="javascript language-javascript">  // src / ragewa. jsshigo {Lissafi, Taswirar} daga 'wanda ba'a iya canzawa';const init = List ([));aikin aikin fitarwa (todos = init, aiki) {canza (nau'in aiki) {akwati 'ADD_TODO':// .akwati 'TOGGLE_TODO':// .tsoho:komawa;}} </code>   </pre>  <p>  Yin aiki da  <code>  ADD_TODO  </code>  mataki ne mai sauki, kamar yadda zamu iya amfani da. hanyar tura  <span class="f-c-white l-mr3"> , wanda zai dawo da sabon jerin tare da lambar da aka haɗa a ƙarshen:  </p>  <pre>   <code class="javascript language-javascript">  yanayin 'ADD_TODO':komawa zuwa. turawa (Taswirar (aikin da aka biya). </code>   </pre>  <p>  Tsayar da cewa muna sake mayar da kayan abu zuwa wani taswirar marar iyaka kafin an tura ta cikin jerin.  </p>  <p>  Muhimmin aikin da muke bukata mu rike shine  <code>  TOGGLE_TODO  </code> :  </p>  <pre>   <code class="javascript language-javascript">  yanayin 'TOGGLE_TODO':komawa zuwa. map (t => {idan (t (samu ("id") === aiki.dawo t. sabuntawa ('isDone', isDone =>! isDone);} da {dawo t;}}); </code>   </pre>  <p>  Muna amfani. taswirar  <span class="f-c-white l-mr3">  don yin amfani da shi a kan lissafi kuma ka sami gado wanda  <code>  id  </code>  ya dace da aikin. Sai muka kira. sabuntawa  <span class="f-c-white l-mr3"> , wanda ya ɗauki maɓalli da aiki, to, ya sake dawo da sabon kundin taswirar, tare da darajar a maɓallin kewayawa tare da sakamakon sakamakon ƙimar farko zuwa aikin ɗaukakawa.  </p>  <p>  Yana iya taimaka wajen ganin rubutun na ainihi:  </p>  <pre>   <code class="javascript language-javascript">  ya kasance = Map ({id: 0, rubutu: 'foo', isDone: ƙarya});todo. sabuntawa ('isDone', isDone =>! isDone);// => {id: 0, rubutu: 'foo', isDone: gaskiya} </code>   </pre>  <h2 id="connectingeverything">  Haɗa kome  </h2>  <p>  Yanzu muna da ayyukan mu da kuma rage shirye, zamu iya ƙirƙirar kantin sayar da kaya da kuma haɗa shi zuwa ga ma'aunin mu:  </p>  <pre>   <code class="javascript language-javascript">  // src / app. jsshigo da amsa daga 'amsa';shigo da [sa} daga 'amsa-dom';shigo {createStore} daga 'rage';shigo {TodoList} daga '. / aka gyara ';shigo da shigo daga '. / rage ';Mabuɗar kantin sayar da kayayyaki = createStore (rage);sa (<TodoList todos = {store. getState  <span class="f-c-white l-mr3"> } />,daftarin aiki. GetElementById ('app')); </code>   </pre>  <p>  Dole ne mu sa abubuwan da aka gyara su san wannan kantin sayar da. Yin amfani da Semalt amfani da ragewa don taimakawa wajen sauƙaƙe wannan tsari. Yana ba mu damar ƙirƙirar kwantena masu ajiya waɗanda ke kunshe da abubuwan da aka gyara, saboda kada mu canza canjin mu na asali.  </p>  <p>  Za mu buƙaci akwati kusa da mu  <code>   <TodoList />   </code>  bangaren. Bari mu ga yadda wannan yayi kama:  </p>  <pre>   <code class="javascript language-javascript">  // src / kwantena. jsshigo da {haɗi} daga 'rage-aikawa';shigo da * kamar yadda aka gyara daga '. / aka gyara ';shigo da {addTodo, toggleTodo} daga '. / ayyuka ';fitarwa na TodoList = gama (aiki mapStateToProps (jihar) {// .},aiki mapDispatchToProps (aika) {// .}) (aka gyara.) TodoList); </code>   </pre>  <p>  Muna ƙirƙirar kwantena tare da aikin haɗi. Idan muka kira  <code>  haɗa  <span class="f-c-white l-mr3">   </code> , zamu wuce biyu ayyuka,  <code>  mapStateToProps  <span class="f-c-white l-mr3">   </code>  da  <code>  mapDispatchToProps  <span class="f-c-white l-mr3">   </code> . manufa;const rubutu = shigarwa. darajar;const neEnterKey = (taron. wanda == 13);const neLongEnough = rubutu. tsawon> 0;idan (shineEnterKey && isLongEnough) {shigarwa. darajar = "';addTodo (rubutu);}};Gudun kunnawaClick = id => taron => toggleTodo (id);dawo ( <div className = 'todo'>  <shigarwa type = "rubutu"className = 'ƙaddamarwa'placeholder = 'Add todo'onKeyDown = {onSubmit} />  <labaran sunaName = 'jerin sunayen'> {takardu. taswirar (t => ( <key key = {t. sami ("id")}className = 'ƙaddamarwa'onClick = {toggleClick (t (get ('id'))}> <Todo todo = {t. toJS  <span class="f-c-white l-mr3"> } /> </li> ))} </ ul>  </div> );} </code>   </pre>  <p>  Kwantena za su karɓa ta atomatik zuwa canje-canje a cikin shagon, kuma za su sake mayar da kayan da aka nannade a duk lokacin da shirin su ya canza.  </p>  <p>  A ƙarshe, muna buƙatar mu sanya kwantena suna sanin kantin sayar da, ta amfani da  <code>   <Mai samar da />   </code>  bangaren:  </p>  <pre>   <code class="javascript language-javascript">  // src / app. jsshigo da amsa daga 'amsa';shigo da [sa} daga 'amsa-dom';shigo {createStore} daga 'rage';shigo da {Mai bayarwa} daga 'rage-aikawa';shigo da shigo daga '. / rage ';shigo {TodoList} daga '. / kwantena ';// ^^^^^^^^^^Mabuɗar kantin sayar da kayayyaki = createStore (rage);sa ( <Kantin sayar da kayayyaki = {store}>  <TodoList />  </ Provider> ,daftarin aiki. GetElementById ('app')); </code>   </pre>  <h3 class="f-c-grey-400">  Shawarar Harsuna  </h3>  <h2 id="conclusion">  Kammalawa  </h2>  <p>  Babu ƙaryatãwa cewa yanayin da ke kewaye da React da Redux zai iya zama mai wuya da tsoratarwa don farawa, amma labari mai dadi shine kusan duk waɗannan batutuwa za su iya canjawa. Mun taba kusantar da gine-ginen Redux, amma riga mun gani sosai don taimakawa mu fara koyi game da Elm Architecture, ko kuma dauke da ɗakin ɗakunan ClojureScript kamar Om ko Re-frame. Hakazalika, kawai mun ga wani ɓangare na yiwuwar tare da bayanai mai ban mamaki, amma yanzu mun fi dacewa don fara koyon harshe irin su Clojure ko Haskell.  </p>  <p>  Ko kana kawai bincika yanayin ci gaban yanar gizon yanar gizo, ko kuna ciyar da rubuce-rubuce a kowace rana, kwarewa tare da kayan aiki da abubuwan da ba a iya yin amfani da shi ba sun riga sun zama masu fasaha ga masu ci gaba, da kuma  <em>  yanzu  </em>  ) lokaci ne mai girma don a koyi abubuwa masu muhimmanci.  </p>  <div class="Article_authorBio l-mv4 t-bg-white m-border l-pa3">  <div class="l-d-f l-pt3">  <img src = "/ img / 6e2f5873a638b37c9799012358afddbe0. com / avatar / 3328d047eacbf158ff38b3c5c7c7fa6b? s = 96 & d = mm & r = g" alt = "Yadda za a gina Todo App ta amfani da React, Redux, da kuma Immutable. jsYadda za a gina Todo App ta amfani da React, Redux, da kuma Immutable. jsRelated Topics:
APIsTools & Semalt
"/>  <div class="f-lh-title">  <div class="f-c-grey-300">  Ku sadu da marubuta  </div>  <div class="f-large"> Dan Prince <i class="fa fa-twitter">   </i>   <i class="fa fa-github">   </i>   </div>  </div>  </div>  <div class="f-light f-lh-copy l-mt3">  Digital Nomad da kuma co-kafa na farko da aka kafa a Ingila Astral Dynamics.  </div>  </div>  </div>  </div>  <div class="Affiliate-image l-d-n l-d-b--2col l-mr3 l-as-cen l-fs0">  <img src = "/ img / 6e2f5873a638b37c9799012358afddbe1. jpg" alt = "Yadda za a gina Todo App ta amfani da React, Redux, da kuma Immutable. jsYadda za a gina Todo App ta amfani da React, Redux, da kuma Immutable. jsRelated Topics:
APIsTools & Semalt
"/>  </div>  <div class="f-c-grey-400 l-d-f l-ai-cen">  <div class="Affiliate-Box">  <div class="f-larger">   <span class="f-bold Affiliate-title">  Hanyar Mafi Kwarewa don Koyi Ayyukan Sahabbai  </span>   </div>  <div class="f-large">  Wes Bos  </div>  <div>  Ɗaukaka horo na mataki zuwa mataki don sa ka gina ainihin duniya React. js + Shirye-shiryen shafukan yanar gizo da shafin yanar gizon da aka tsara a cikin wasu lokuta. Yi amfani da code coupon  <strong>  'SITEINTINT'  </strong>  a wurin biya don samun  <strong>  25% kashe  </strong> .  </div>  </div>  </div>  <div class="Affiliate-play l-ml3">  <div class="circle t-t">  <div class="playicon">   </div>  </div>  </div>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </span>  </div>  </p>  </todo>  </todo>  </todo>  </todolist>  </todolist>  </todolist>  </todolist>  </todolist>  </buga>  </shigarwa>  </shigarwa>  </labaran>  </labaran>  </key>  </key>  </html>  </kai>  </link>                                           
March 1, 2018