Cov txheej txheem:

React Intermediate Tutorial: 3 Kauj Ruam
React Intermediate Tutorial: 3 Kauj Ruam

Video: React Intermediate Tutorial: 3 Kauj Ruam

Video: React Intermediate Tutorial: 3 Kauj Ruam
Video: Starlight and Astrophysics: Mindblowing Science - PANEL - TAG Con 2020 Virtual Intergalactic 2024, Hlis ntuj nqeg
Anonim
React Intermediate Tutorial
React Intermediate Tutorial
React Intermediate Tutorial
React Intermediate Tutorial

React Intermediate Tutorial

Saib cov khoom tiav ntawm no.

Koj yuav kawm dab tsi?

Koj yuav tsim cov npe yooj yim-rau-ua nrog React.js, thiab kawm txog qhov nyuaj dua ntawm qhov tshwm sim. Yam yuavtsum tau kawm uantej (tau txais kev pom zoo) ua tiav qhov react.js tau pib qhia.

Khoom siv

Txhua qhov software yuav raug them hauv qhov kev qhia.

Koj yuav xav tau lub khoos phis tawj nrog cov software hauv qab no teeb tsa:

  • npm/yarn
  • IDE uas txhawb nqa js
  • Ib lub web browser

Kauj Ruam 1: Qhia Tiav Qhia Nruab Nrab

Pib Pib

Vim li cas React.js?

Nrog React.js, lub ntsiab lus yog rov siv cov cai. Piv txwv li, hais tias koj muaj lub bar qhia uas koj muaj hauv 100 nplooj ntawv. Yog tias koj xav tau ntxiv nplooj ntawv tshiab, tom qab ntawd koj yuav tsum hloov pauv kab lus qhia ntawm txhua nplooj ntawv, txhais tau tias koj yuav tsum ua tib yam rau 100 nplooj ntawv. Txawm hais tias nrog macros, qhov no dhau los ua kev lom zem heev.

Txhim Kho Cov Software/Pob Ntawv Yuav Tsum Muaj

Koj yuav xav tau:

npm/yarn

Yuav ua li cas rau nruab:

  1. Mus thiab teeb tsa qhov tseeb LTS ntawm Node.js
  2. YOOJ YIM: yog tias koj xav tau xov paj li koj tus thawj tswj hwm pob, nruab xov paj los ntawm kev ntaus rau hauv powershell npm nruab -g xov
  3. Qhib powershell/cmd.exe
  4. Nkag mus rau cov npe uas koj xav tsim koj txoj haujlwm hauv
  5. Ntaus npx create-react-app.

Koj tau ua tiav theem teeb tsa. los sim nws, qhib powershell, mus rau koj cov npe phau ntawv, thiab ntaus npm pib. koj yuav tsum tau txais lub vev xaib thauj khoom ntawm koj lub browser default.

Kauj ruam 2: Kauj Ruam 1: Pib

Kauj ruam 1: Pib
Kauj ruam 1: Pib

Txhawm rau pib tawm, rho tawm cov ntaub ntawv hauv qab no los ntawm koj /src cov npe:

  • App.test.js
  • index.css
  • logo.svg
  • serviceWorker.js
  • setupTests.js

Peb tsis xav tau cov ntaub ntawv no.

Cia peb tseem npaj peb cov fileystem. Tsim cov npe no hauv /src /:

  • js
  • css ua

muab App.js rau hauv js dir thiab App.css rau hauv css dir.

Tom ntej no, cia peb rov txhim kho qhov kev vam khom.

hauv index.js, tshem tawm cov khoom xa tuaj rau serviceWorker thiab index.css. Rho tawm serviceWorker.register (). Hloov txoj hauv kev rau App.

hauv App.js, tshem qhov ntshuam rau logo.svg, vim peb tsis xav tau nws ntxiv lawm. Rov qhib App.css. rho tawm App () muaj nuj nqi thiab xa tawm rau App.

Hauv React, peb muaj 2 txoj hauv kev los txheeb xyuas cov ntsiab lus. Peb muaj cov haujlwm thiab chav kawm. kev ua haujlwm yog rau cov khoom tsis yooj yim dua, thiab cov chav kawm feem ntau yog rau cov khoom sib txawv. Vim tias daim ntawv teev npe todo nyuaj dua li pawg ntawm HTML, peb yuav siv cov qauv kawm hauv chav kawm.

Ntxiv qhov no rau koj cov cai:

pastebin.com/nGXeCpaH

html yuav nyob hauv 2 divs.

cia peb txhais Element.

pastebin.com/amjd0jnb

ceeb toom seb peb txhais tus nqi li cas hauv lub xeev. Peb yuav xav tau qhov no tom qab.

hauv qhov ua haujlwm muaj nuj nqi, hloov hi nrog {this.state.value}

peb tab tom ua tus nqi dhau los ntawm lub xeev uas peb tau hais tseg.

yog li cia peb sim nws!

hauv qhov ua haujlwm ntawm App, hloov nws nrog qhov no:

pastebin.com/aGLX4jVE

nws yuav tsum tso saib tus nqi: "xeem".

cia saib seb peb puas tuaj yeem ua ntau txoj Haujlwm!

tsis txhob tau txais React los ua ib qho hauv ib qho xwb, peb tuaj yeem tsim ib qho array ntawm thiab qhia cov tshuaj tiv thaiv kom ua cov array hloov chaw.

hloov qhov ua haujlwm ntawm qhov no:

pastebin.com/05nqsw71

qhov no yuav tsum ua 10 qhov haujlwm sib txawv. Pom tias peb ntxiv cov yuam sij li cas. Cov yuam sij no tau siv los ua tus cim rau kev tawm tsam thiab peb, yog peb xav tau lawv.

Tam sim no tias peb daim ntawv teev npe ua haujlwm tau ua haujlwm, peb nrhiav txoj hauv kev los thauj cov haujlwm. Nov yog qhov uas peb lub xeev tuaj.

cia ntxiv tus tsim rau peb.

pastebin.com/9jHAz2AS

Hauv tus neeg tsim khoom no, peb tau hloov lub taskArray kom deb ntawm qhov ua haujlwm mus rau hauv lub xeev. rho tawm taskArray thiab rau lub voj hauv kev ua haujlwm. hloov taskArray hauv div rau hauv no.state.taskArray.

Txog tam sim no, koj App.js code yuav tsum zoo li no:

pastebin.com/1iNtUnE6

Kauj Ruam 3: Ntxiv Txoj Hauv Kev Ntxiv thiab Tshem Tawm Cov Khoom

Cia peb ntxiv txoj hauv kev ntxiv thiab tshem tawm cov khoom. Cia peb npaj nws tawm.

Peb xav tau dab tsi?

  • Ib txoj hauv kev rau tus neeg siv ntxiv cov khoom
  • Ib qhov chaw khaws khoom
  • Ib txoj hauv kev los khaws cov khoom

Peb yuav siv dab tsi?

  • Ib yam
  • Localstorage API w/ JSON

Cia peb pib nrog cov ntsiab lus nkag.

hauv qab {this.state.taskArray}, ntxiv cov lus nkag thiab khawm rau koj cov cai

Ntxiv

Yuav tsum muaj cov ntawv nkag thiab khawm ntxiv tam sim no.

Nws tsis muaj dab tsi tam sim no, yog li cia ntxiv 6 txoj hauv kev rau peb App txoj hauv kev.

Peb xav tau txoj hauv kev rau thaum lub pob nyem, thiab kuj thaum qee tus neeg ntaus hauv cov lus nkag. Peb kuj xav tau txoj hauv kev los tsim cov haujlwm ua haujlwm, ntxiv rau txuag, thauj khoom, thiab tshem tawm cov haujlwm.

wb ntxiv 6 txoj hauv kev no:

khawm Nyem ()

inputTyped (evt)

generateTaskArray ()

saveTasks (paub tab)

getTasks ()

tshem tawm Task (id)

cia peb ntxiv qhov sib txawv no rau peb lub xeev:

tswv yim

Peb kuj yuav tsum khi peb lub luag haujlwm rau qhov no.

pastebin.com/syx465hD

Cia peb pib ntxiv kev ua haujlwm.

ntxiv 2 tus cwj pwm zoo li no:

qhov no ua rau nws kom thaum tus neeg siv yam dab tsi hauv kev tawm tswv yim, nws ua tiav txoj haujlwm.

ntxiv onClick tus yam ntxwv rau Ntxiv zoo li:

Ntxiv

thaum tus neeg siv nyem lub pob, txoj haujlwm ua haujlwm.

tam sim no tias ntu html tau ua tiav, cia peb pib nrog kev ua haujlwm.

Kuv twb tau sau uantej sau LocalStorage API interface, yog li hloov cov saveTasks, getTasks, thiab tshem tawm Task ua haujlwm nrog qhov no:

pastebin.com/G02cMPbi

cia peb pib ntawm kev ua haujlwm inputTyped.

thaum tus neeg siv hom, peb yuav tsum hloov pauv tus nqi sab hauv ntawm cov tswv yim.

cia peb ua qhov ntawd los ntawm kev siv setState muaj nuj nqi muab nrog cov lus teb.

this.setState ({input: evt.target.value});

txoj kev no, peb tuaj yeem tau txais tus nqi ntawm cov tswv yim.

ib zaug ua tiav, peb tuaj yeem ua haujlwm ntawm lub pob nyem Ua haujlwm.

peb yuav tsum tau ntxiv ib txoj haujlwm rau daim ntawv teev npe ua haujlwm. peb xub rub cov npe ua haujlwm los ntawm localStorage, kho nws, thiab tom qab ntawd khaws nws. Peb tom qab ntawd hu xov tooj rau rerender ntawm taskList los hloov kho nws.

var taskList = this.getTasks ();

taskList.tasks.push (this.state.input);

this.saveTasks (taskList);

this.generateTaskArray ();

peb tau txais cov haujlwm, thawb tus nqi nkag mus rau hauv cov haujlwm, thiab tom qab ntawd khaws nws. Tom qab ntawd peb tsim cov haujlwm ua haujlwm.

tam sim no, cia ua haujlwm ntawm generateTaskArray () muaj nuj nqi.

peb yuav tsum:

  • tau cov haujlwm
  • tsim ib qho array ntawm cov haujlwm ua haujlwm
  • hla cov haujlwm ua haujlwm los ua

peb tuaj yeem tau txais cov haujlwm thiab khaws cia hauv qhov sib txawv nrog getTasks ()

var cov haujlwm = getTasks (). cov haujlwm

tom qab ntawd peb yuav tsum tsim ib qho array thiab ntxiv nws.

pastebin.com/9gNXvNWe

nws yuav tsum ua haujlwm tam sim no.

SOURCE CODE:

github.com/bluninja1234/todo_list_instructables

EXTRA IDEAS:

Muaj nuj nqi tshem tawm (yooj yim heev, ntxiv onclick thiab rho tawm siv tshem tawm Task los ntawm qhov ntsuas tseem ceeb)

CSS (kuj yooj yim, sau koj tus kheej lossis siv bootstrap)

Pom zoo: