投稿時間:2023-04-24 02:09:56 RSSフィード2023-04-24 02:00 分まとめ(14件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
IT 気になる、記になる… コンビニ各社、合計5,000円以上の「Apple Gift Card」購入で最大10%分を還元するキャンペーンを開始(5月7日まで) https://taisy0.com/2023/04/24/171067.html applegiftcard 2023-04-23 16:19:45
IT 気になる、記になる… AppleのMRヘッドセットのバッテリーは外付け − バッテリー駆動時間は約2時間 https://taisy0.com/2023/04/24/171065.html apple 2023-04-23 16:11:33
python Pythonタグが付けられた新着投稿 - Qiita Atcoder典型90問 001 - Yokan Party https://qiita.com/T08Y02/items/92044f7e0c32e9e3f319 atcoder 2023-04-24 01:49:44
python Pythonタグが付けられた新着投稿 - Qiita 【LINE APIと天気予報API】を活用して天気予報を毎朝自動送信する https://qiita.com/kazuki_178/items/bb9f5f05415249cfabf1 lineapi 2023-04-24 01:10:11
Ruby Rubyタグが付けられた新着投稿 - Qiita 数値を0で割った時にZeroDivisionErrorが返ってこない件 https://qiita.com/kanaji-r/items/b6451398a5a3d1f00bdb begin 2023-04-24 01:13:27
Git Gitタグが付けられた新着投稿 - Qiita とりあえずメモ https://qiita.com/ueki5/items/6edd37b0499a56ae971d kfixcmdpostgrepcwindowgit 2023-04-24 01:09:04
海外TECH DEV Community Simplifying state management with useReducer hook https://dev.to/vivekalhat/simplifying-state-management-with-usereducer-hook-50e4 Simplifying state management with useReducer hookState management is a fundamental aspect of React React provides different native ways to manage and maintain state One of the most commonly used way to manage a state is useState hook When you start your journey of learning hooks in React useState will probably be your first destination The useState hook provides a simple API to manage a component state Below is an example of useState hook const count setCount useState The useState hook returns two variables count an actual statesetCount a state updater functionA React component generally contains the logic that returns some JSX i e the logic to render UI When you add state management to the component then it contains both logic for rendering a UI and managing the state When a component gets big it becomes quite challenging to maintain both state management and UI rendering logic Although this works totally fine still a React component should separate the UI and state management logic Let s look at the below example import useState from react import v as uuidv from uuid const ListState gt const defaultTodo id uuidv title Write a new blog isComplete false id uuidv title Read two pages of Rework isComplete true id uuidv title Organize playlist isComplete false const todo setTodo useState defaultTodo const item setItem useState Marks a given todo as complete or incomplete const handleTodoChange id gt const updatedTodos todo map item gt if item id id return item isComplete item isComplete return item setTodo updatedTodos Adds a new todo item to the list const handleAddItem e gt e preventDefault const updatedTodos todo id uuidv title item isComplete false setTodo updatedTodos setItem Removes todo item from the list based on given ID const handleDeleteItem id gt const updatedTodos todo filter item gt item id id setTodo updatedTodos return lt div className container gt lt div className new todo gt lt input placeholder Add New Item value item onChange e gt setItem e target value className todo input gt lt button onClick handleAddItem className add todo gt Add lt button gt lt div gt lt ul className todo list gt todo length gt todo map item gt lt li key item id className list item gt lt input type checkbox checked item isComplete onChange gt handleTodoChange item id gt lt p gt item title lt p gt lt button onClick gt handleDeleteItem item id className delete todo gt Delete lt button gt lt li gt lt p gt List is empty lt p gt lt ul gt lt div gt export default ListState The above code renders a simple Todo list where a user can do the following things Add a new todo itemDelete a todo itemMark a todo as complete incomplete The above component contains both the logic for handling state and rendering JSX React provides a native hook called useReducer using which we can separate the logic for UI and state management A useReducer hook is an alternative to useState hook What is useReducer The useReducer hook allows you to add state management to your component similar to useState hook The only difference is that it does this by using a reducer pattern The API for useReducer hook is as follows const state dispatch useReducer reducerFn initialState The useReducer hook takes two parameters A reducer function that contains the logic for state management An initial stateThe useReducer hook returns two variables The current stateA dispatch function which when called invokes the reducer function to update the state Let s learn to use this hook by refactoring the above Todo list component import useState useReducer from react import ListReducerFn from utils functions import defaultTodo from utils defaults const ListReducer gt const todo dispatch useReducer ListReducerFn defaultTodo const item setItem useState Marks a given todo as complete or incomplete const handleTodoChange id gt dispatch type UPDATE payload id Adds a new todo item to the list const handleAddItem e gt e preventDefault dispatch type ADD payload item setItem Removes todo item from the list based on given ID const handleDeleteItem id gt dispatch type DELETE payload id return lt div className container gt lt div className new todo gt lt input placeholder Add New Item value item onChange e gt setItem e target value className todo input gt lt button onClick handleAddItem className add todo gt Add lt button gt lt div gt lt ul className todo list gt todo length gt todo map item gt lt li key item id className list item gt lt input type checkbox checked item isComplete onChange gt handleTodoChange item id gt lt p gt item title lt p gt lt button onClick gt handleDeleteItem item id className delete todo gt Delete lt button gt lt li gt lt p gt List is empty lt p gt lt ul gt lt div gt export default ListReducer utils defaults jsimport v as uuidv from uuid export const defaultTodo id uuidv title Write a new blog isComplete false id uuidv title Read two pages of Rework isComplete true id uuidv title Organize playlist isComplete false utils functions jsimport v as uuidv from uuid export const ListReducerFn state action gt switch action type case ADD const updatedTodos state id uuidv title action payload item isComplete false return updatedTodos case DELETE const updatedTodos state filter item gt item id action payload id return updatedTodos case UPDATE const updatedTodos state map item gt if item id action payload id return item isComplete item isComplete return item return updatedTodos default throw new Error Unsupported action action type As you can see the JSX for both versions of Todo list component is same The only difference is in the logic for handling state management Let s discuss the differences between the Todo list component that uses useState and useReducer hooks Here is a function to add a new todo list item written using useState logic Adds a new todo item to the list useState version const handleAddItem e gt e preventDefault const updatedTodos todo id uuidv title item isComplete false setTodo updatedTodos setItem In the above code handleAddItem function is used for adding a new todo item to the list We are creating a new array of updated todo list items and setting the state with updated todo list that also includes newly added item We are updating the state directly from the function itself Let s look at the useReducer version of the same function Adds a new todo item to the list useReducer version const handleAddItem e gt e preventDefault dispatch type ADD payload item setItem The above function does the same thing i e it adds a new todo item to the list In this scenario the state management logic is separated and we are not directly updating the state from the function Instead here we are using dispatch function provided by useReducer to dispatch an action Whenever an action is dispatched the reducer function is called to modify and return the new state based on the type of action What is dispatch function A dispatch is a special function that dispatches an action object It basically acts as a request to update the state The dispatch function takes an action object as a parameter You can pass anything to dispatch function but generally you should pass only the useful information to update the state Here is a sample action object const action type ADD payload todo Write a new blog article The action object should generally contain following two properties type It basically tells reducer what type of action has happened eg ADD DELETE etc payload optional An optional property with some additional information required to update the state The dispatch function invokes the reducer function to update and return the new state The logic to update the state is done inside the reducer function instead of a React component In this way the state management can be separated from UI rendering logic What is a reducer function A reducer function handles all the logic of how a state should be modified It takes two parameters A current stateAn actionFollowing is a sample reducer function that handles the logic of updating todo list state export const ListReducerFn state action gt switch action type case ADD const updatedTodos state id uuidv title action payload item isComplete false return updatedTodos case DELETE const updatedTodos state filter item gt item id action payload id return updatedTodos case UPDATE const updatedTodos state map item gt if item id action payload id return item isComplete item isComplete return item return updatedTodos default throw new Error Unsupported action action type Whenever an action is dispatched from a component the reducer function is invoked to update and return the new state TL DRThe useState and useReducer hooks allow you to add state management to the React component The useReducer hook provides more flexibility as it allows you to separate UI rendering and state management logic You can check the code for above sample here 2023-04-23 16:26:42
Apple AppleInsider - Frontpage News Flexispot BS11Pro office chair review: stylish and comfortable https://appleinsider.com/articles/23/04/23/flexispot-bs11pro-office-chair-review-stylish-and-comfortable?utm_medium=rss Flexispot BSPro office chair review stylish and comfortableFlexispot s increasingly wide range of office chairs now includes this ergonomic BSPro swivel chair which aims to help make extended sitting periods more bearable Nothing can make an over long sitting session good for you but focusing on posture is good for even short periods in the chair It s immediately comfortable for instance and gives you a sense of being able to spring up out of it rather than sink too far in Flexispot BSPro ーdesign Read more 2023-04-23 16:04:40
Apple AppleInsider - Frontpage News When Apple's headset launches, it will do more than Oculus https://appleinsider.com/articles/23/04/23/when-apples-headset-launches-it-will-do-more-than-oculus-does?utm_medium=rss When Apple x s headset launches it will do more than OculusApple will be including many different features in its inbound mixed reality headset with it expected to offer many elements in its first incarnation that other headsets don t provide AppleInsider render of the forthcoming Apple VR headsetThe Apple VR and AR headset is expected to land at WWDC in June and become the center of attention While speculation has largely centered around its hardware capabilities it seems that its list of features and functions could be just as expansive Read more 2023-04-23 16:20:15
ニュース BBC News - Home Sudan violence: UK diplomats evacuated from Khartoum https://www.bbc.co.uk/news/uk-65367019?at_medium=RSS&at_campaign=KARANGA khartoumhundreds 2023-04-23 16:26:35
ニュース BBC News - Home Diane Abbott suspended as Labour MP after racism letter https://www.bbc.co.uk/news/uk-politics-65365978?at_medium=RSS&at_campaign=KARANGA abbott 2023-04-23 16:11:29
ニュース BBC News - Home Head's sister hits out at Ofsted boss for defending inspection https://www.bbc.co.uk/news/uk-65365001?at_medium=RSS&at_campaign=KARANGA doubt 2023-04-23 16:29:56
ニュース BBC News - Home Watch moment sirens start on phones across the UK https://www.bbc.co.uk/news/uk-65368844?at_medium=RSS&at_campaign=KARANGA people 2023-04-23 16:04:30
ニュース BBC News - Home CBI rape allegations shocking says Labour shadow minister https://www.bbc.co.uk/news/business-65364950?at_medium=RSS&at_campaign=KARANGA ashworth 2023-04-23 16:34:37

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)