You addItem/removeItem has a fairly unlikely race condition in it.
You are reading the list from the state, modifying it, then using this.setState. However, this.setState does not update the state immediately.
In theory, addItem could get called twice in quick succession, and you would lose one of the items added. For example with this possible ordering of events:
Initial List ["a", "b", "c"]
addItem Called with "d", modified list ["a", "b", "c", "d"], setState called
addItem called with "e", modified list ["a", "b", "c", "e"], setState called
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
That's not necessarily true. I know plenty of enterprit level projects that use React that were created with CRA. As long as you configure and secure it, it's the best solution for making a start on a React project.
I wanted to share this thought with you cause I didn't give enough attention about differences between componentWillReceiveProps and getDerivedStateFromProps and IMHO this is kind of important.
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
Really liked your tutorial Tim! <3 I thought I try all of that with functional components and this is what I got (feel free to correct me - I'm kinda rookie in webdev). Seems working though!
Minor changes I made:
used functional components instead of class components. Therefore used React.useState() for storing variables in the state.
input element for adding new todo is a controlled component and handled also in the state. Found this approach more "react-ish".
for deleting a todo I also used filter() method - seems more handy for me.
some naming changes
App.js
// App.jsimportReact,{useState,Fragment}from"react";importListfrom"./List";constApp=()=>{const[userInput,setUserInput]=useState("");const[list,setList]=useState(["walk the dog","buy the milk","learn some code"]);// userinput is controlled by the App componentconsthandleChange=e=>{setUserInput(e.target.value);};constaddItem=e=>{if(userInput!==""){setList([...list,userInput]);setUserInput("");}};constremoveItem=item=>{constupdatedList=list.filter(listItem=>listItem!==item);setList(updatedList);};return(<Fragment><Listlist={list}removeItem={removeItem}/><hr/><form><inputplaceholder="Something that needs to be done..."value={userInput}onChange={handleChange}/><buttontype="button"onClick={addItem}>{'Add Item'}</button></form></Fragment>);}exportdefaultApp;
"Fair warning: This part may get a bit abstract, so do your best to stick with me. If I don't explain well enough or you're struggling with it, leave a comment and I'll try to help you get it." ~ T. Smith
I love the generous spirit and the acknowledgement that sometimes what we think we're say/writing is not what is heard/understood.
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
Software engineer with 4+ years of experience in building products for numerous domains like fin-tech, real estate, video streaming, retail, and now e-commerce.
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
I’m a full stack developer who has experience with several front-end tools like Reactjs, Vuejs, and jQuery as well as some back-end tools like PHP, Laravel, Node, and Express.
Location
IL
Education
AAS in Information Technology/Web Development
Work
Senior Software Development Engineer at Wizards of the Coast
I am a budding CS engineer who works on C++, java. loves to code and create Stuff. starting my journey towards full stack web development. Open to healthy advice and tips :)
Hey! nice article. but how do we implement search via using two keywords. like i have rows of some information. i want to search using keywords age and gender. any idea?
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for the article.
You
addItem/removeItemhas a fairly unlikely race condition in it.You are reading the
listfrom the state, modifying it, then usingthis.setState. However,this.setStatedoes not update the state immediately.In theory,
addItemcould get called twice in quick succession, and you would lose one of the items added. For example with this possible ordering of events:addItemCalled with "d", modified list["a", "b", "c", "d"],setStatecalledaddItemcalled with "e", modified list["a", "b", "c", "e"],setStatecalled["a", "b", "c", "d"]["a", "b", "c", "e"]Just something to keep in mind.
Interesting. How would you suggest preventing that issue in a project like this?
Use call back format for setState.
Great tutorial, but why not use facebook cra instead ?
If cra is your choice, you can try c-r-a-p 💩-create-react-app-parcel 😃
I have not heard of this. It looks cool though. Thanks for the link!
It’s really personal preference, but to be honest I haven’t used create-react-app in a while and forgot it existed.
At the end It's better to make it your own way. create-react-app is for fast dev or examples but not for real projects.
That's not necessarily true. I know plenty of enterprit level projects that use React that were created with CRA. As long as you configure and secure it, it's the best solution for making a start on a React project.
Nice Article.
When I use getDerivedStateFromProps() in same way you use componentWillReceiveProps() in List component searching doesn't work.
static getDerivedStateFromProps(nextProps) {
return {
filtered: nextProps.items
};
}
I didn't know that getDerivedStateFromProps is called every time we setState(). Link for lifecycles projects.wojtekmaj.pl/react-lifecy....
I wanted to share this thought with you cause I didn't give enough attention about differences between componentWillReceiveProps and getDerivedStateFromProps and IMHO this is kind of important.
This is awesome! Thanks for the link!
Np. Thank you for the article
Hi there,
Really liked your tutorial Tim! <3 I thought I try all of that with functional components and this is what I got (feel free to correct me - I'm kinda rookie in webdev). Seems working though!
Minor changes I made:
used functional components instead of class components. Therefore used React.useState() for storing variables in the state.
input element for adding new todo is a controlled component and handled also in the state. Found this approach more "react-ish".
for deleting a todo I also used
filter()method - seems more handy for me.some naming changes
App.js
List component:
default
create-react-appindex.js..."Fair warning: This part may get a bit abstract, so do your best to stick with me. If I don't explain well enough or you're struggling with it, leave a comment and I'll try to help you get it." ~ T. Smith
I love the generous spirit and the acknowledgement that sometimes what we think we're say/writing is not what is heard/understood.
Thanks for the article.
FYI, I don't see the instructions to modify the
package.jsonto include thestartscript.Thanks! I have added the piece about scripts in the package.json file.
There's a typo:
mkdir search-tasks && $_should bemkdir search-tasks && cd $_Fixed. Thank you!
Hi Tim,
I found typo error at Next we need to install the necessary packages to our package. => Next we need to install the necessary packages to our project
Corrected. Good catch. It sounded like I was trying to be xhibit or something.
Great Article Thanks...
No problem. I’m glad it was helpful!
If you get an error when importing like --> import 'bulma/bulma' use --> import 'bulma/css/bulma.css'
how to add not found text when the user didn't find any related results?
view here - loom.com/share/59eed867b4594ace89e...
Hey! nice article. but how do we implement search via using two keywords. like i have rows of some information. i want to search using keywords age and gender. any idea?