{"id":36,"date":"2024-10-07T17:38:52","date_gmt":"2024-10-07T17:38:52","guid":{"rendered":"https:\/\/berlin.nandrin.net\/?p=36"},"modified":"2025-09-29T07:43:25","modified_gmt":"2025-09-29T07:43:25","slug":"react-reducer","status":"publish","type":"post","link":"https:\/\/berlin.nandrin.net\/index.php\/react\/react-reducer\/","title":{"rendered":"React Reducer"},"content":{"rendered":"\n<p>Quick demo for React Reducer usage.<br>You do not use the React.useState() function anymore.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react'\nexport function counterReducer(state,action) {\n    let newState = {...state};\n    if(action.type==='INCREMENT'){\n        newState.count = state.count+1;\n    }\n    if(action.type==='DECREMENT'){\n        newState.count = state.count-1;\n    }\n    if(action.type==='RESET'){\n        newState.count = 0;\n    }\n    return newState;\n}\n\nfunction App() {\n    const &#91;state, dispatch]= React.useReducer(counterReducer,{count:0,tag:'test'});\n  return (\n    &lt;div id=\"app\"&gt;\n      &lt;h1&gt;The (Final?) Counter&lt;\/h1&gt;\n      &lt;p id=\"actions\"&gt;\n        &lt;button onClick={()=&gt;dispatch({type:'INCREMENT'})}&gt;Increment&lt;\/button&gt;\n        &lt;button onClick={()=&gt;dispatch({type:'DECREMENT'})}&gt;Decrement&lt;\/button&gt;\n        &lt;button onClick={()=&gt;dispatch({type:'RESET'})}&gt;Reset&lt;\/button&gt;\n      &lt;\/p&gt;\n      &lt;p id=\"counter\"&gt;{state.count} ({state.tag})&lt;\/p&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Quick demo for React Reducer usage.You do not use the React.useState() function anymore.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20],"tags":[22],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-react","tag-react"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":2,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/posts\/36\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/berlin.nandrin.net\/index.php\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}