IT |
気になる、記になる… |
Microsoft、「Surface Book 3」向けに2021年4月度のファームウェアアップデートをリリース |
https://taisy0.com/2021/04/11/138825.html
|
microsoft |
2021-04-11 13:24:36 |
python |
Pythonタグが付けられた新着投稿 - Qiita |
Python機械学習プログラミング備忘録 3.1-3.2 |
https://qiita.com/soyeiji1220/items/b70fb9d621e04c678fd1
|
下の方にある、「誤分類されたサンプルは黄色でプロット」以下が誤分類されたサンプルを描画するためのコードです。 |
2021-04-11 22:58:20 |
js |
JavaScriptタグが付けられた新着投稿 - Qiita |
Javascript 備忘録4<関数> |
https://qiita.com/akamaru/items/851b269f027235dc108c
|
Javascript備忘録関数functionanumconsolelognum上の関数名と同じfunctionanumreturnnum→Javascriptの場合は、関数名が被った場合、後に宣言された関数が優先される。 |
2021-04-11 22:46:14 |
js |
JavaScriptタグが付けられた新着投稿 - Qiita |
javascriptでsplitを使ってコンマで分割すると、""で囲まれた内部のコンマまで分割されてしまう。 |
https://qiita.com/hatorijobs/items/dd0c730e6faba0c84203
|
このようなCSVを分割した場合にがとに分かれてしまう。 |
2021-04-11 22:46:06 |
Program |
[全てのタグ]の新着質問一覧|teratail(テラテイル) |
Djangoのmanage.pyがホストサーバとして上手く動作しません |
https://teratail.com/questions/332722?rss=all
|
Djangoのmanagepyがホストサーバとして上手く動作しませんDjangonbspmanagepyの実行について初歩的な質問失礼します。 |
2021-04-11 22:53:56 |
Program |
[全てのタグ]の新着質問一覧|teratail(テラテイル) |
MacVim上で日本語の文字入力を“.”でリピートする方法 |
https://teratail.com/questions/332721?rss=all
|
MacVim上で日本語の文字入力を“でリピートする方法前提・実現したいことMacVimで日本語をでリピート入力したいと思っています。 |
2021-04-11 22:44:27 |
Program |
[全てのタグ]の新着質問一覧|teratail(テラテイル) |
android studio。正方形のウィジェットの作り方。 |
https://teratail.com/questions/332720?rss=all
|
androidstudio 。 |
2021-04-11 22:17:43 |
Program |
[全てのタグ]の新着質問一覧|teratail(テラテイル) |
固定画面でのスクロール検知方法について |
https://teratail.com/questions/332719?rss=all
|
固定画面でのスクロール検知方法について前提下記の参考サイトのようなデザインの実装方法に興味があるのですが、現在の知識とデベロッパーツールによる調査では実現方法がわかりませんでした。 |
2021-04-11 22:07:26 |
Program |
[全てのタグ]の新着質問一覧|teratail(テラテイル) |
oracleDBで画像を登録する方法 |
https://teratail.com/questions/332718?rss=all
|
oracleDBで画像を登録する方法初心者です。 |
2021-04-11 22:03:44 |
Docker |
dockerタグが付けられた新着投稿 - Qiita |
APIモックに便利なjson-serverをdockerを使って構築してみる |
https://qiita.com/kiyo27/items/51df5eb0b383e4048ba3
|
jsonserverはnpmでインストールするのですが、開発マシンを汚したくないときにはdockerを使って構築したいなと思いますよね。 |
2021-04-11 22:15:01 |
海外TECH |
Ars Technica |
Unknowns linger for idea of scattering sunlight to cool the Earth |
https://arstechnica.com/?p=1755823
|
earthnew |
2021-04-11 13:30:17 |
海外TECH |
Ars Technica |
A new documentary highlights the visionary behind space settlement |
https://arstechnica.com/?p=1755700
|
basic |
2021-04-11 13:00:46 |
海外TECH |
DEV Community |
Simple add to cart animation with CSS & JS |
https://dev.to/nikhil27b/simple-add-to-cart-animation-with-css-js-39e
|
Simple add to cart animation with CSS amp JSHello today I created a Simple add to cart animation with CSS amp JS Not great work but I hope you like this also comments about your thoughts For more content follow me on Instagram developer nikhil Thank you |
2021-04-11 13:53:24 |
海外TECH |
DEV Community |
The React Tutorial for 2021: A Complete Guide |
https://dev.to/reedbarger/the-react-tutorial-for-2021-a-complete-guide-47oo
|
The React Tutorial for A Complete GuideWelcome to a tutorial made to help you become effective with React as quickly as possible as you build a complete application along the way As compared to many tutorials you might have gone through before this one is meant to be thoroughly practical from start to finish You will learn how to create an entire React application all within around lines of code which makes use of many of the core concepts of React hooks state management forms JSX elements components props styling and conditionals And best of all you will learn all of these concepts while coding yourself hands on Let s get started Looking for the complete guide to becoming a hired React developer Check out the React Bootcamp How to bootstrap our React projectWe re going to create our React application by going to the website react new What this will do is create a new code sandbox for us We can use code sandbox to create and develop complete React applications without having to install anything on our computer Once you visit react new you will see your code editor and on the right hand side we see a live version of our application to which we can make changes Quick tip Make sure to hit command ctrl S Doing so will fork our sandbox and create a special URL that we can revisit in the future Right now we re looking at our app component which is the only component that s being displayed in our application If we look at our file explorer on the left we ll see app is being imported and rendered here within this index js file src index jsimport StrictMode from react import ReactDOM from react dom import App from App const rootElement document getElementById root ReactDOM render lt StrictMode gt lt App gt lt StrictMode gt rootElement What does all of this code do It simply renders or displays our app by putting injecting it into an index html file which is what we see on the right hand side of the page The code also finds and puts our app in the so called root element a div with the id of root If you want see where that element is you can find it within our public folder specifically in the index html file How to use JSXNow that we have a working React app let s start building it and changing what we see Let s begin within our div by removing this h element and within our h just calling our app Todo List What we are working with here is called JSX It looks very similar to HTML but is in fact JavaScript We use it to build the structure of our application just as we would use HTML We can use any standard HTML elements within JSX divs any heading element paragraph spans buttons etc It s important to note that there are some minor differences between JSX and HTML The attributes that we use on JSX are slightly different than in normal HTML elements They are written in the camelcase style which is a standard way of writing variables or properties in JavaScript For example to apply a class on a JSX element we use an attribute called className For normal HTML that would just be called class src App jsimport styles css export default function App return lt div className App gt lt h gt Todo List lt h gt lt div gt If we use class instead of className for JSX we re going to get a warning saying class is an invalid DOM property Create list of todo elementsSince we re making a todo application let s create our todo list underneath our h header We could begin by making an unordered list with some list items as children elements Each todo would be listed within an li element src App jsimport styles css export default function App return lt div className App gt lt h gt Todo List lt h gt lt ul gt lt li gt Todo Item lt li gt lt ul gt lt div gt We can do something better as React developers however Instead let s make a dedicated component that is responsible for displaying our todos Create new React componentsComponents are the backbone of any React application We use components to separate different parts of our user interface This makes them reusable wherever we need them across our app better organizes our code and makes it easier to understand our projects Components fulfill an important concept in programming which is called separation of concerns This means it is preferable for each part of our component to have its own clearly defined role and responsibilities separate from any other component Just as we have an App component we can create a component to be displayed within App Since it is a list of todos let s call it TodoList src App jsimport styles css export default function App return lt div className App gt lt h gt Todo List lt h gt lt TodoList gt component with single tag lt div gt React component rulesEvery component must begin with a capital letter and once a component is declared it can be written and used very similarly to an HTML element would A component can consist of just one tag or two tags If it doesn t have anything between the two tags which are called children it should only have as one tag as the code above displays lt TodoList gt Additionally if a component or element consists of just one tag it must be self closing Meaning it must end in a forward slash i e lt TodoList gt and not lt TodoList gt We are attempting to display our TodoList component but we haven t created it yet To do that we can create another function component like App with the name TodoList At this point we re going to get this error saying nothing was returned from render We need to return something specifically some JSX Every component we make must return JSX elements and components which must also ultimately be composed of JSX In our case we want to return our list of todos Let s take our unordered list with all of our list items that we want to show We don t really have any data just yet so let s create some In particular let s create a set of todo data which we can include in an array Let s add this to the App component src App jsimport styles css export default function App const todos id text Wash dishes done false id text Do laundry done false id text Take shower done false return lt div gt lt h gt Todo List lt h gt lt TodoList gt lt div gt function TodoList Pass data to components with propsNow the question is how do we pass all this data to and display it within our todo list With React components we can do that with special properties that we add to the component called props Props are custom attributes we can add to React components to pass data to our components They are the React equivalent of arguments in JavaScript Since our data is called todos let s name our prop the same todos We use the equals operator to set a props value as well as a set of curly braces This is because our todos array is a variable a dynamic value src App jsimport styles css export default function App const todos id text Wash dishes done false id text Do laundry done false id text Take shower done false return lt div gt lt h gt Todo List lt h gt lt TodoList todos todos gt lt div gt function TodoList If we wanted to make it a string for example we would wrap it in a set of quotes but since this is a dynamic value that can change we want to always include it within curly braces Within the TodoList component where are our props going to be received to ultimately display our todos data They re going to be received exactly where any function would receive their arguments We receive our prop data on an object which we usually call props but we can give it whatever name we like We can see that we re passing this data down by using console log props If look at our console tab we have this property on our props object called todos It has an array of three items just like we would expect src App jsimport styles css export default function App const todos id text Wash dishes done false id text Do laundry done false id text Take shower done false return lt div gt lt h gt Todo List lt h gt lt TodoList todos todos gt lt div gt function TodoList props console log props todos Array Map over array items with the map functionIn order to display each of these list items we can take the array that is on props todos In particular we can use a special function that React gives us on the todos array called map Since we want to display this within TodoList we once again need to use a set of curly braces to display it within our JSX Using props todo map we will map over this array just like we would a normal JavaScript array The React map function is slightly different than the normal JavaScript map function because it is made to return and render JSX elements map accepts an inner function and in that function we can get access to each todo Using an arrow function we can return each to do within its own JSX Finally we can immediately return that JSX by wrapping it in a set of parentheses Within our inner function we get access to each todo s data To display that data we can take each todo which we know is an object We can use a set of curly braces to output the dynamic value of whatever is on todo text When we do that we can see our three todos React keys and why they matterIf we look at the console tab at the bottom we will see a warning saying each child in the list should have a unique key prop The reason for this is that React needs to keep track of the order of each of the items in our list It does so with the help of a special React prop called a key For a key you generally want to use a unique identifier a unique value that is only associated with one piece of data In our case to identify each todo s data we will use the unique number provided on todo id So why are keys important It is important for React to figure out how it should appropriately update our user interface If we were to update a todo s text or done value the key is what tells React which todo item needs to be updated Once we add the key prop to the element or component that we re looping over we no longer get that warning Get individual props with destructuringNote that one additional shorthand is that instead of referencing the entire object within the TodoList we can reference the individual properties on that object to make our code a little bit shorter by using object destructuring Object destructuring is not a React concept but a standard JavaScript feature that makes accessing object properties either by immediately declaring them as individual variables As of right now we only have one prop being passed down to TodoList so let s destructure that one prop todos individually To do so we add a set of curly braces within our functions parameters and just grab the property that we need from the props object This means that we can change props todos to just todos src App jsimport styles css export default function App const todos id text Wash dishes done false id text Do laundry done false id text Take shower done false return lt div gt lt h gt Todo List lt h gt lt TodoList todos todos gt lt div gt using object destructuring on the props objectfunction TodoList todos return lt ul gt todos map todo gt lt li key todo id gt todo text lt li gt lt ul gt Add new todo list itemsNow what about adding some new todos to our list Underneath our TodoList component let s add a new component that s responsible for adding new todos A logical name for this would be AddTodo We can create this underneath our to do list component Let s have AddTodo return a form element that contains a basic text input and a submit button src App jsimport styles css export default function App const todos id text Wash dishes done false id text Do laundry done false id text Take shower done false return lt div gt lt h gt Todo List lt h gt lt TodoList todos todos gt lt AddTodo gt lt div gt function TodoList todos return lt ul gt todos map todo gt lt li key todo id gt todo text lt li gt lt ul gt function AddTodo return lt form gt lt input placeholder Add todo gt lt button type submit gt Submit lt button gt lt form gt Note that any JSX element that consists of just one tag such as our input must end in a forward slash If we do not include it we re going to get a compiler error saying unterminated JSX contents Now the question is how do we type into our input submit our form and have a new todo added to our todos array Handle form submissions in ReactTo take care of submitting our form we need to start working with events in React In our case we want to use the submit event when our form is submitted by our user and for React to handle that form submission by adding a new todo React adds a special prop to the form element called onSubmit onSubmit accepts a function within a set of curly braces Let s create a new function which we will call handleAddTodo Note that most functions that handle events in React are prefixed with the word handle It s ultimately up to you how you want to name your functions but this is a helpful convention It s important to note that this function should be created within the component itself AddTodo not outside of it When handleAddTodo is passed to the onSubmit prop it will be called when our form is submitted src App jsimport styles css function AddTodo function handleAddTodo return lt form onSubmit handleAddTodo gt lt input placeholder Add todo gt lt button type submit gt Submit lt button gt lt form gt Prevent default form behaviorWhen we click the submit button or hit the return key data from the submit event is passed automatically to our function that s connected to onSubmit We receive that event data in the parameters of handleAddTodo The first thing that we want to do with this event is call a method on it called preventDefault What this method does is prevents the default action whenever we submit a form src App jsimport styles css function AddTodo function handleAddTodo event event preventDefault return lt form onSubmit handleAddTodo gt lt input placeholder Add todo gt lt button type submit gt Submit lt button gt lt form gt Whenever we submit a form by default the page is refreshed We don t want that behavior with React we want JavaScript to control whatever happens next After preventing a refresh we want to get access to what was typed into the input to create a new todo with it How do we do that Access form data on submitThe way that we get access to all of the elements within our form is with the help of the property event target elements This will give us first of all the event target which is the form itself elements is a property that will give us all of the elements within that form including our input and our submit button If we were to console log event target elements right now submit our form and look at our console we see just an object with a couple of properties one called and one called This isn t very helpful to us although we do see that it is our input and our button Instead we want to get what was typed into our input To do so we can add either an id or a name attribute to our input Let s add the the name attribute with a value of addTodo What this is going to do when we hit submit again is give us a new property on the elements object also called addTodo From that reference we can very easily get what was typed into it This allows us to use event target elements addTodo value to get what was typed in whatever text was typed in When we do so when we type text into our input and hit submit we see it logged to the console Now that we have our text we ll put it in a variable called text Using this we want to create a new todo We know that each todo is an object and it has to consist of the properties id text and done Let s create a variable todo and that will be equal to a new object where the id will be the text will be equal to the text that we re getting from the elements object and we can set done to false By default new todos that are added are not going to be done src App jsimport styles css function AddTodo function handleAddTodo event event preventDefault const text event target elements addTodo value const todo id text done false return lt form onSubmit handleAddTodo gt lt input name addTodo placeholder Add todo gt lt button type submit gt Submit lt button gt lt form gt And finally the big question is how do we add this todo to our array todos Introduction to React stateThis is where the concept of state comes in Right now we re dealing with static data there is no real way to update this todos array To be clear there is a way to do it using JavaScript but what we are not currently able to do is tell React even if we were to update it that it needs to re render this list In other words to perform an update to our data and then show us the updated data in our view So while we could update the data we also need React to show our users the updated data State is required to fix our problem State is a means of managing our application data and also allows React to update our UI user interface in response to data changes Manage state with the useState hookWe can manage state in React using the useState hook To use the useState hook the first thing that we need to do is import React up at the top because useState comes from the core React library After that we can simply call the useState hook up at the top of our app component Once we call useState just like a normal function we will pass in our entire array of todos as our initial data Our application will break for a moment since we re no we re no longer showing our todos just yet useState returns an array with two elements The initial value we called useState with our array of todos and this becomes our state variable A special function that allows us to update what is stored in the state variableWe can destructure the values that are returned from useState by adding a set of array brackets to immediately get the values that are returned from it First the state and second the function to update the state We ll call our state variable todos and the setter to manage our state setTodos All we have to do to update our state is to pass it whatever we want the new state to be This setTodos function is going to be passed down to our AddTodo component so let s add that as a prop of the same name We ll also destructure setTodos from our props object within AddTodo And finally we can call setTodos at the bottom of handleAddTodo What s great about this function is instead of having to pass down the todos array as well this function can give us the previous state with the help of a function that we can receive inside of it This may seem strange at first but within setTodos we get access to the previous todo data If we write an arrow function or any function for that matter we can simply provide what we want the new state to be The benefit of being able to access the previous state variable s value directly within the setter function is that it prevents us from having to pass down the entire todos state variable as an additional prop to every component in which we want to update its value If we wanted to empty our todos state we could just return an empty array right here If we were to submit our form we can see that all of our todos are removed Once we submit our form state is updated and our app is re rendered as a result Re renders in ReactNote that any re render within a parent component will cause any child components to re render That means whenever our todo data is updated the TodoList component a child of the App component is updated with that new data If we go back to handleAddTodo we can take our previous todos and use the concat method to add this new todo to our array in state All we have to do is return this expression Let s add a new todo such as Balance Checkbook Once we hit submit we see that immediately added to our list Now there s one problem here we aren t clearing out our input after our form is submitted This means if we wanted to add another todo we would have to manually clear it out How do we take this input s value and clear it out React refs and useRefTo perform common actions such as clearing out an input s value or focusing our input can be done with the help of what s called a ref A ref is a feature that React provides to reference to a given DOM element In this case we want a reference to this input element with the name of addTodo Just like our state we can work with refs by calling the appropriate React hook To create a ref we just need to call React useRef at the top of AddTodo We don t have to pass it an initial value but we can give it a default value if we needed to We will call this created ref inputRef Using inputRef we can create a reference to our input element which we can access wherever we like by using the built in ref prop by setting ref inputRef src App jsimport React from react import styles css function AddTodo setTodos const inputRef React useRef function handleAddTodo event event preventDefault const text event target elements addTodo value const todo id text done false setTodos prevTodos gt return prevTodos concat todo return lt form onSubmit handleAddTodo gt lt input name addTodo placeholder Add todo ref inputRef gt lt button type submit gt Submit lt button gt lt form gt What does this do It allows us within handleAddTodo to use the property inputRef current which contains input element itself If we were to log input ref current we would see our input element We have a direct reference to our input which means we access any property that we like off of it In our case we want to take the value of the input on the value property To clear the value from our input we can just mutate inputRef directly by setting value to an empty string Whenever we hit submit our input is cleared out without having to clear it out ourselves manually Essential rules of React hooksSince useRef is another React hook we re starting to see some common features among React hooks They are often prefixed with the word use In fact most all React hooks have this prefix to denote that they are hooks and should be used as such Additionally React hooks are called up at the very top of function components Hooks cannot be used within class components And finally hooks cannot be conditionally i e within an if statement But as you can see there s nothing too special about React hooks They operate very much like regular JavaScript functions Toggling todos done with onClickAfter creating todos we want to toggle them done to strike through them if we ve finished a given todo How do we add this feature If we go back to our list item within TodoList we can see what that will look like by applying some inline styles We saw how to add styles through classes For styles that we want to apply inline to any given element we cannot use the same syntax as we would with normal HTML If we tried to using the HTML syntax we re going to get an error telling us the style prop expects style properties within an object not within a string To fix this we will provide an object We need to provide this object within another set of curly braces Then we will provide any property like we would in a normal JavaScript object to apply this strike through style For each of our list items we can set the property textDecoration to line through We do not want every item to be struck through we only want this to be applied if a given todo is done How do we do that We can use a normal JavaScript conditional in particular a ternary to say if a given to do if it s property done is true then we want to apply the strike through value for text decoration otherwise not If we change one of our todos array to have a done value of true we see that that rule that style rule is applied src App js function TodoList todos return lt ul gt todos map todo gt lt li style textDecoration todo done line through key todo id gt todo text lt li gt lt ul gt How do we actually toggle that todo We might want our user to click or double click on our todo in order to strike through it That means we want to see how to register and handle a new type of event a click event To handle a click event with React is by providing the onClick prop to a given element for which we want to register that event In this case it s the li element Once again we need to connect it to a function to handle our click event We re going to call this handleToggleTodo and create it within our TodoList component In this case our function that we use to handle the event doesn t have to receive any event data This function will handle updating our todos state We want handleToggleTodo to go through the todos array and see if the one that the user has clicked on exists in our array If so its done value can be toggled to the opposite boolean value To receive the appropriate todo data for the appropriate list item that is clicked on we can call handleToggleTodo as inline arrow function and pass the todo data as an argument src App js function TodoList todos function handleToggleTodo todo return lt ul gt todos map todo gt lt li onClick gt handleToggleTodo todo style textDecoration todo done line through key todo id gt todo text lt li gt lt ul gt To update our todos state we ll pass down setTodos to our TodoList component We ll pass down setTodos as a prop to TodoList and destructure it from the props object Once again we can call setTodos and get access to the previous todos by including an inner function First what we can do is take our entire todos array and map over it with the map array function In the inner function passed to map we will check that the todos id we re mapping over is equal to the todo that we ve clicked on If so we return a new object with all of the previous todo s properties previous to dues properties but with done toggled to its opposite boolean value src App js function TodoList todos setTodos function handleToggleTodo todo confused by this code Here s what it says if a todo s id is equal to the one we clicked on just update that todo s done value to its opposite otherwise do nothing return it const updatedTodos todos map t gt t id todo id t done t done t return lt ul gt todos map todo gt lt li onDoubleClick gt handleToggleTodo todo style textDecoration todo done line through key todo id gt todo text lt DeleteTodo todo todo setTodos setTodos gt lt li gt lt ul gt Otherwise if that todo that we re iterating over is not the one that we clicked on we just want to return it without changing it This updated array is what we ll pass to setTodos to update our state If we click on a todo we toggle it done If we click on it again it s toggled back to undone For this to work appropriately to see that a past todos id is equal to the todo that we re clicking on we need to make sure that each todos id is unique Instead of setting each new todo to have an id of we can just use Math random to make a semi random value and ensure there are no list items with the same id Finally as an alternative to onClick we can use another event prop onDoubleClick in the event that users accidentally click a given todo Now if a user double clicks a list item only then do we toggle it done Handle deleting todosThe final bit of functionality that we re looking for is to be able to delete a given todo We can add that functionality within TodoList by adding another nested component Underneath our todo text we ll add a new component DeleteTodo Let s declare this new component this above where we declared AddTodo What will this component consist of In it we will return a span which will function like a button for us A user can click on this and delete a given todo If you want a non button element to operate like a button we need to make its role property set to button To our span let s add some style rules we can give it a color of red make it bold and separate it from the todo text by setting marginLeft What s neat about the style object is that we don t have to say pixels as a string we can use the value or include any integer that we like Here is the code for our DeleteTodo component so far To delete a todo we want to be able to click on it and show a confirmation dialog If the user confirms they want to delete it only then is the todo removed Since we re mapping over each todo item including DeleteTodo we can pass down a prop called just todo with each todo s data on it In DeleteTodo on our span element we want to add an onClick to handle deleting our todo To handle this we will call a new function handleDeleteTodo Using this function we first want to show a confirmation dialog We can do so by saying window confirm with the message Do you want to delete this window confirm is going to return a value of true or false based on whether the user has confirmed the dialog or not We ll put the result of this action in a variable called confirmed src App js function TodoList todos setTodos return lt ul gt todos map todo gt lt li onDoubleClick gt handleToggleTodo todo style textDecoration todo done line through key todo id gt todo text pass todo data down as a prop to DeleteTodo lt DeleteTodo todo todo gt lt li gt lt ul gt function DeleteTodo todo setTodos function handleDeleteTodo const confirmed window confirm Do you want to delete this if confirmed take care of deleting the todo return lt span onClick handleDeleteTodo role button style color red fontWeight bold marginLeft cursor pointer gt x lt span gt If confirmed is true only then do we want to delete the todo To do that we need to use setTodos once again We ll pass it down one more level from TodoList to the DeleteTodo component and destructure it from the props object Then within handleDeleteTodo we can call it and use the inner function to get the previous todos To remove the todo that a user has clicked on we can filter through this array to make sure that we are removing the one that we selected that the user selected To do so we make sure all the todos in our array do not have an id equal to the one we are attempting to delete src App js function DeleteTodo todo setTodos function handleDeleteTodo const confirmed window confirm Do you want to delete this if confirmed setTodos prevTodos gt return prevTodos filter t gt t id todo id return lt span onClick handleDeleteTodo role button style color red fontWeight bold marginLeft cursor pointer gt x lt span gt Now if we attempt to delete one of our todos we see our confirmation dialog we hit ok and immediately it s removed from our list If we delete all of our todos we no longer see anything If we want to tell our user there are no todos in the list when the array is empty let s head up to our TodoList component If we have an empty todos array we can add a conditional above our return and check if our array s length is equal to If so we will display a paragraph element with the text No todos left function TodoList todos setTodos function handleToggleTodo todo const updatedTodos todos map t gt t id todo id t done t done t setTodos updatedTodos if todos length return lt p gt No todos left lt p gt return lt ul gt todos map todo gt lt li onDoubleClick gt handleToggleTodo todo style textDecoration todo done line through key todo id gt todo text lt DeleteTodo todo todo setTodos setTodos gt lt li gt lt ul gt Congratulations You now have a working todo app that has full CRUD functionality to be able to create read update and delete todos You re able to see how many of the major React concepts work firsthand and you re now in a great position to start building your own React applications If you would like to take a look at our final app code you can see it here Ready for the next step Join The React BootcampThe React Bootcamp takes everything you should know about learning React and bundles it into one comprehensive package including videos cheatsheets plus special bonuses Gain the insider information hundreds of developers have already used to master React find their dream jobs and take control of their future Click here to be notified when it opens |
2021-04-11 13:46:53 |
海外TECH |
DEV Community |
Make a simple React app with Webpack - An easy practical guide |
https://dev.to/napoleon039/make-a-simple-react-app-with-webpack-an-easy-practical-guide-3nb3
|
Make a simple React app with Webpack An easy practical guideCreate React App is the first choice of most if not all React developers It creates a React project for us and only requires a few commands Its simplicity and quick nature make it a favorite among beginners as well But there are also ways to create a React app without it One of these ways is using a module bundler like Webpack and a compiler like Babel By the end of this article you will have your very own React app without using create react app Dependencies and project structureSince this React app will use the Webpack module bundler we need to install quite a few dependencies These dependencies are required by Webpack so it can detect and work with the various file types Here s what we need to install These two packages are the dependencies we need for our simple app If your project needs any other packages you may install those There are close to devDependencies so let s install them in groups and I ll explain what each dependency is for First comes webpack webpack Installs the webpack module bundlerwebpack cli Offers a variety of commands that make it easier to work with webpack on the command linewebpack dev server Allows us to use a simple web server with hot reloadNext we ll install Babel babel core Core package for the Babel compiler babel preset env A smart preset that allows us to use the latest JavaScript syntax babel preset react As the name suggests it transpiles React code to plain JavaScriptbabel loader A plugin that enables Webpack to work with Babel and its presetsThe remaining devDependencies are for CSS and HTML style loader css loader Required so that webpack can detect css fileshtml webpack plugin Generates an HTML file that includes all your Webpack bundles via script tagsEverything a basic React project needs is installed Now create these folders and file in the project s root A public folder for the HTML file and assets images fonts etc A src folder for js files and React componentsA babel config json Babel configuration fileAt this point your project folder should look something like this Webpack and Babel configLet s add the presets in the Babel config file It is recommended to have two separate config files for Webpack one for development and one for production Although both files will have the same configuration for loaders and any plugins there are slight differences This is what a config file for development looks like Here we first declare the mode configuration option With this Webpack can use its built in optimizations accordingly Next module rules is an array containing objects This rule tells Webpack to look for files ending in js and use Babel to compile them babel loader helps Webpack work with Babel The second rule tells Webpack to look for files ending in css and make sense of them with the help of two loaders style loader and css loader The last rule helps Webpack recognize image files There is no need to install any external loader for this Note According to the Webpack docs style loader and css loader need to be used in the exact same order as in this config file or it won t work After loaders we have two plugins When Webpack bundles all JavaScript CSS and other necessary files it also creates an HTML file This is where we use the html webpack plugin This plugin tells Webpack to use our HTML file as a template and inject the compiled bundles into it So instead of creating its own HTML file Webpack instead uses our HTML file public index html and adds the bundled files to it via lt script gt tags The other plugin we use is HotModuleReplacementPlugin which comes with the webpack package and is used for hot reloading our app in development mode Finally the devServer object contains options used by webpack dev server Now let s take a look at the production version of the configuration file Not much has changed except that we no longer need HotModuleReplacementPlugin and devServer since they will not be used in production The mode option has also been set accordingly You might have noticed the changes in output output filename has a value that we haven t seen before The values in square brackets are tokens The name token allows Webpack to name files differently if we use code splitting contenthash is used so that the bundle file name changes when its content changes Running the appOur app is almost ready I have a simple App js component The app looks like this now Finally we need to add scripts to run our app With the serve option Webpack uses webpack dev server to create a web server Let s run our app Open https localhost in your browser and your app should be running Wrapping upThis is a basic React app where we manually configure Webpack I hope this helped you to understand a bit about the various plugins and loaders Webpack requires and what its configuration files look like The Webpack documentation is worth reading It was the major resource I used for this article In comparison Parcel uses very few dependencies I wrote another article about creating a React app with Parcel That said both have their own pros and cons Here are links to some additional resources Webpack documentation on config files and the various options a config file can haveWebpack documentation on loading CSS and other assetsWebpack documentation on webpack dev server and html webpack pluginAn article from Carl Rippon on creating a React app that uses TypeScript and ESLint with Webpack Babel documentation on its config files |
2021-04-11 13:26:59 |
海外TECH |
CodeProject Latest Articles |
WPF Datagrid with Footer Aggregate |
https://www.codeproject.com/Tips/5299459/WPF-Datagrid-with-Footer-Aggregate
|
formats |
2021-04-11 13:59:00 |
海外ニュース |
Japan Times latest articles |
Japan to start COVID-19 vaccinations for elderly Monday |
https://www.japantimes.co.jp/news/2021/04/11/national/covid-19-japan-vaccinations-vaccines-elderly-health/
|
nation |
2021-04-11 23:06:30 |
海外ニュース |
Japan Times latest articles |
Resurgent Rikako Ikee set for big Olympic role in hometown |
https://www.japantimes.co.jp/sports/2021/04/11/more-sports/swimming/rikako-ikee-olympics/
|
japan |
2021-04-11 22:30:28 |
ニュース |
BBC News - Home |
Prince Philip: The Queen says his death has 'left a huge void' - Duke of York |
https://www.bbc.co.uk/news/uk-56710086
|
windsor |
2021-04-11 13:44:26 |
ニュース |
BBC News - Home |
NI riots: Adult influence in disorder 'amounts to child abuse' |
https://www.bbc.co.uk/news/uk-northern-ireland-56708430
|
children |
2021-04-11 13:55:18 |
ニュース |
BBC News - Home |
Burnley 1-2 Newcastle United: Saint-Maximin inspires Magpies turnaround at Turf Moor |
https://www.bbc.co.uk/sport/football/56629083
|
Burnley Newcastle United Saint Maximin inspires Magpies turnaround at Turf MoorAllan Saint Maximin inspires Newcastle to a vital win at Burnley that lifts them six points clear of the relegation zone |
2021-04-11 13:51:11 |
ニュース |
BBC News - Home |
Prince Philip: What are the plans for his funeral? |
https://www.bbc.co.uk/news/uk-56694327
|
edinburgh |
2021-04-11 13:27:36 |
LifeHuck |
ライフハッカー[日本版] |
貯金できない…。お金にまつわる目標はいつ立て直すのがベスト? |
https://www.lifehacker.jp/2021/04/start-money-goals-on-your-birthday-not-january.html
|
貯金できない…。 |
2021-04-11 22:05:00 |
コメント
コメントを投稿