{"componentChunkName":"component---node-modules-rocketseat-gatsby-theme-docs-core-src-templates-docs-query-js","path":"/react-js/states","result":{"data":{"mdx":{"id":"1c3d5643-4967-54b2-930e-bc8fee806e71","excerpt":"Introduction to React Hooks What are React Hooks? React Hooks have a very simple API, but given its massive community and variety of use cases, questions are…","fields":{"slug":"/react-js/states/"},"frontmatter":{"title":"State in React","description":"State | useState | React | RefugeesCode","image":null,"disableTableOfContents":null},"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"State in React\",\n  \"description\": \"State | useState | React | RefugeesCode\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", {\n    \"id\": \"introduction-to-react-hooks\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#introduction-to-react-hooks\",\n    \"aria-label\": \"introduction to react hooks permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Introduction to React Hooks\"), mdx(\"h3\", {\n    \"id\": \"what-are-react-hooks\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#what-are-react-hooks\",\n    \"aria-label\": \"what are react hooks permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"What are React Hooks?\"), mdx(\"p\", null, \"React Hooks have a very simple API, but given its massive community and variety of use cases, questions are bound to arise around React Hooks best practices and how to solve common problems. Hooks let you use different React features from your components. You can either use the built-in Hooks or combine them to build your own.\"), mdx(\"p\", null, \"\\uD83D\\uDCC4 Read about \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://blog.logrocket.com/react-hooks-cheat-sheet-solutions-common-problems/#usestate\"\n  }, \"React Hooks cheat sheet: Best practices and examples\")), mdx(\"h3\", {\n    \"id\": \"state-hooks\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#state-hooks\",\n    \"aria-label\": \"state hooks permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"State Hooks\"), mdx(\"p\", null, \"State lets a component \\u201Cremember\\u201D information like user input. For example, a form component can use state to store the input value, while an image gallery component can use state to store the selected image index. \"), mdx(\"p\", null, \"\\uD83D\\uDCC4 Read more about \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://react.dev/learn/state-a-components-memory\"\n  }, \"State\"), \" in the official page of React.dev\"), mdx(\"p\", null, \"To add state to a component, use one of these Hooks:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"useState\"), \" declares a state variable that you can update directly. \", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"Note:\"), \" This the one we will see in deep in this section.\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"li\"\n  }, \"useReducer\"), \" declares a state variable with the update logic inside a reducer function.\")), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"function ImageGallery() {\\n  const [index, setIndex] = useState(0);\\n  // ...\\n}\\n\")), mdx(\"h2\", {\n    \"id\": \"goals\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#goals\",\n    \"aria-label\": \"goals permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Goals\"), mdx(\"ul\", {\n    \"className\": \"contains-task-list\"\n  }, mdx(\"li\", {\n    parentName: \"ul\",\n    \"className\": \"task-list-item\"\n  }, mdx(\"input\", {\n    parentName: \"li\",\n    \"type\": \"checkbox\",\n    \"checked\": false,\n    \"disabled\": true\n  }), \" \", \"Add a state variable to your component\"), mdx(\"li\", {\n    parentName: \"ul\",\n    \"className\": \"task-list-item\"\n  }, mdx(\"input\", {\n    parentName: \"li\",\n    \"type\": \"checkbox\",\n    \"checked\": false,\n    \"disabled\": true\n  }), \" \", \"Track state in a function component\"), mdx(\"li\", {\n    parentName: \"ul\",\n    \"className\": \"task-list-item\"\n  }, mdx(\"input\", {\n    parentName: \"li\",\n    \"type\": \"checkbox\",\n    \"checked\": false,\n    \"disabled\": true\n  }), \" \", \"Manage simple local states within a component\")), mdx(\"h2\", {\n    \"id\": \"basic-tools\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#basic-tools\",\n    \"aria-label\": \"basic tools permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Basic tools\"), mdx(\"p\", null, \"In this course we already learned about class and functional components in \", \"[section 1 - React.js]\", \" (\", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://refugeescode-materials.netlify.app/react-js/introduction#reviewing-how-the-project-is-structured\"\n  }, \"https://refugeescode-materials.netlify.app/react-js/introduction#reviewing-how-the-project-is-structured\"), \"). Functional components are functions that accept arguments as the properties of the component and return valid JSX, as shown below:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"function Message(props) {\\n  return <div>{props.message}</div>\\n}\\n// Or as an arrow function\\nconst Message = (props) =>  <div>{props.message}</div>\\n\")), mdx(\"p\", null, \"As you can see, there are no state or lifecycle methods. However, as of React v16.8, we can use Hooks. \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://blog.logrocket.com/react-reference-guide-hooks-api/\"\n  }, \"React Hooks\"), \" are functions that add state variables to functional components and instrument the lifecycle methods of classes. They tend to start with \", mdx(\"strong\", {\n    parentName: \"p\"\n  }, \"use\"), \" .\"), mdx(\"h2\", {\n    \"id\": \"contents\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#contents\",\n    \"aria-label\": \"contents permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Contents\"), mdx(\"h3\", {\n    \"id\": \"usestate\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#usestate\",\n    \"aria-label\": \"usestate permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"UseState\"), mdx(\"p\", null, \"As seen in the introduction section, the React \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" Hook allows you to have state variables in functional components. You pass the initial state to this function, and it returns a variable with the current state value (not necessarily the initial state) and another function to update this value. \"), mdx(\"p\", null, \"\\uD83C\\uDFA5 More info in this \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://www.youtube.com/watch?time_continue=1&v=4qVNaohzDWU&embeds_referring_euri=https%3A%2F%2Fblog.logrocket.com%2F&source_ve_path=Mjg2NjY&feature=emb_logo\"\n  }, \"video\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" is React Hook that allows you to add state to a functional component(link to our material). It returns an array with two values: the current state and a function to update it. The Hook takes an initial state value as an argument and returns an updated state value whenever the setter function is called. It can be used like this:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"const [state, setState] = useState(initialValue);\\n\")), mdx(\"p\", null, \"Here, the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"initialValue\"), \" is the value you want to start with, and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"state\"), \" is the current state value that can be used in your component. The \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"setState\"), \" function can be used to update the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"state\"), \", triggering a re-render of your component.\"), mdx(\"h3\", {\n    \"id\": \"what-can-usestate-hold\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#what-can-usestate-hold\",\n    \"aria-label\": \"what can usestate hold permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"What can \", mdx(\"inlineCode\", {\n    parentName: \"h3\"\n  }, \"useState\"), \" hold?\"), mdx(\"p\", null, \"In React, \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" can store any type of value, whereas the state in a class component is limited to being an object. This includes primitive data types like \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"string\"), \", \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"number\"), \", and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Boolean\"), \", as well as complex data types such as \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"array\"), \", \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"object\"), \", and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"function\"), \". It can even cover custom data types like class instances.\"), mdx(\"p\", null, \"Basically, anything that can be stored in a JavaScript variable can be stored in a state managed by \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \".\"), mdx(\"h3\", {\n    \"id\": \"updating-objects-and-arrays-in-usestate\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#updating-objects-and-arrays-in-usestate\",\n    \"aria-label\": \"updating objects and arrays in usestate permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Updating objects and arrays in \", mdx(\"inlineCode\", {\n    parentName: \"h3\"\n  }, \"useState\")), mdx(\"p\", null, \"Never directly modify an object or array stored in useState. Instead, you should create a new updated version of the object or array and call setState with the new version. \"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"// Objects\\nconst [state, setState] = useState({ name: 'John', age: 30 });\\n\\nconst updateName = () => {\\n  setState({ ...state, name: 'Jane' });\\n};\\n\\nconst updateAge = () => {\\n  setState({ ...state, age: state.age + 1 });\\n};\\n\\n// Arrays\\nconst [array, setArray] = useState([1, 2, 3, 4, 5]);\\n\\nconst addItem = () => {\\n  setArray([...array, 6]);\\n};\\n\\nconst removeItem = () => {\\n  setArray(array.slice(0, array.length - 1));\\n};\\n\")), mdx(\"p\", null, \"Live example: \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://codesandbox.io/p/sandbox/exercise-usestate-hook-1jvy0?file=%2Fsrc%2Findex.js\"\n  }, \"add new elements to a list\")), mdx(\"h3\", {\n    \"id\": \"what-does-the-reactusestate-do\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#what-does-the-reactusestate-do\",\n    \"aria-label\": \"what does the reactusestate do permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"What does the \", mdx(\"inlineCode\", {\n    parentName: \"h3\"\n  }, \"React.useState\"), \" do?\"), mdx(\"p\", null, \"Calling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"React.useState\"), \" inside a function component generates a single piece of state associated with that component.\"), mdx(\"p\", null, \"Whereas the state in a class is always an object, with Hooks, the state can be any type. Each piece of state holds a single value: an \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"object\"), \", an \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"array\"), \", a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Boolean\"), \", or any other type you can imagine.\"), mdx(\"h3\", {\n    \"id\": \"declaring-state-update-state-state-variable\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#declaring-state-update-state-state-variable\",\n    \"aria-label\": \"declaring state update state state variable permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Declaring state, update state, state variable\"), mdx(\"p\", null, \"Refer to these tutorials for a detailed understanding of these 3 concepts:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react/#declaring-state-react\"\n  }, \"Declaring state in React\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react/#using-react-hooks-update-state\"\n  }, \"Using React Hooks to update the state\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react/#implementing-object-state-variable-usestate-hook\"\n  }, \"Implementing an object as a state variable with useState Hook\"))), mdx(\"h4\", {\n    \"id\": \"exercises-🧑\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h4\",\n    \"href\": \"#exercises-%F0%9F%A7%91\",\n    \"aria-label\": \"exercises 🧑 permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Exercises* \\uD83D\\uDC69\\u200D\\uD83D\\uDCBB\\uD83E\\uDDD1\\u200D\\uD83D\\uDCBB:\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Simple Counter: Create a basic counter using the useState hook. Implement buttons to increment and decrement the counter value.\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"User Input Form: Build a form with useState to capture and display user input. For example, capture the user's name and display a greeting.\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Toggle Button: Develop a toggle button that changes its state with the useState hook when clicked. It should change between \\\"On\\\" and \\\"Off\\\" states.\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"Color Picker: Create a color picker using useState to change the background color of a div based on the user's color selection.\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"To-Do List: Build a simple to-do list using the useState hook to add and remove items.\")), mdx(\"p\", null, \"*Taken from \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://drop-x-out.medium.com/mastering-reacts-usestate-hook-20-practical-practice-questions-e3fb3488a8a1\"\n  }, \"Mastering React\\u2019s useState Hook: 20 Practical Practice Questions\")), mdx(\"h4\", {\n    \"id\": \"additional-material-on-these-subjects\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h4\",\n    \"href\": \"#additional-material-on-these-subjects\",\n    \"aria-label\": \"additional material on these subjects permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Additional material on these subjects:\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react/#update-state-nested-object\"\n  }, \"How to update state in a nested object in React with Hooks\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react/#multiple-variables-one-state\"\n  }, \"Working with multiple state variables or one state object\"))), mdx(\"h3\", {\n    \"id\": \"rules-for-using-usestate\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#rules-for-using-usestate\",\n    \"aria-label\": \"rules for using usestate permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Rules for using \", mdx(\"inlineCode\", {\n    parentName: \"h3\"\n  }, \"useState\")), mdx(\"p\", null, mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" abides by the same \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://legacy.reactjs.org/docs/state-and-lifecycle.html\"\n  }, \"rules that all Hooks follow\"), \":\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Only call Hooks at the top level\"), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"Only call Hooks from React functions\")), mdx(\"p\", null, \"The second rule is easy to follow. Don\\u2019t use \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" in a class component:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"class App extends React.Component {\\n  render() {\\n    const [message, setMessage] = useState( '' );\\n\\n    return (\\n      <p>\\n        <strong>{message}</strong>\\n      </p>\\n    );\\n  }\\n}\\n\")), mdx(\"p\", null, \"Or regular JavaScript functions (not called inside a functional component):\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"function getState() {\\n  const messageState = useState( '' );\\n  return messageState;\\n}\\nconst [message, setMessage] = getState();\\nconst Message = () => {\\n /* ... */\\n}\\n\")), mdx(\"p\", null, \"You\\u2019ll get an \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://legacy.reactjs.org/warnings/invalid-hook-call-warning.html\"\n  }, \"error\"), \". The first rule means that even inside functional components, you shouldn\\u2019t call useState in loops, conditions, or nested functions because React relies on the order in which useState functions are called to get the correct value for a particular state variable.\"), mdx(\"p\", null, \"In that regard, the most common mistake is to wrap useState calls in a \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://blog.logrocket.com/react-conditional-rendering-9-methods/\"\n  }, \"conditional statement\"), \" (they won\\u2019t be executed all the time):\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-jsx\"\n  }, \"if (condition) { // Sometimes it will be executed, making the order of the useState calls change\\n  const [message, setMessage] = useState( '' );\\n  setMessage( aMessage );  \\n}\\nconst [list, setList] = useState( [] );\\nsetList( [1, 2, 3] );\\n\")), mdx(\"p\", null, \"A functional component can have many calls to \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" or other Hooks. Each Hook is stored in a list, and there\\u2019s a variable that keeps track of the currently executed Hook.\"), mdx(\"p\", null, \"When \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"useState\"), \" is executed, the state of the current Hook is read (or initialized during the first render), and then, the variable is changed to point to the next Hook. That\\u2019s why it is important to always maintain the Hook calls in the same order. Otherwise, a value belonging to another state variable could be returned.\"), mdx(\"p\", null, \"In general terms, here\\u2019s an example of how this works step by step:\"), mdx(\"ol\", null, mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React initializes the list of Hooks and the variable that keeps track of the current Hook\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React calls your component for the first time\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React finds a call to useState, creates a new Hook object (with the initial state), changes the current Hook variable to point to this object, adds the object to the Hooks list, and returns the array with the initial state and the function to update it\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React finds another call to useState and repeats the actions of the previous step, storing a new Hook object and changing the current Hook variable\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"The component state changes\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React sends the state update operation (performed by the function returned by useState) to a queue to be processed\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React determines it needs to re-render the component\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React resets the current Hook variable and calls your component\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React finds a call to useState, but this time, since there\\u2019s already a Hook at the first position of the list of Hooks, it just changes the current Hook variable and returns the array with the current state, and the function to update it\"), mdx(\"li\", {\n    parentName: \"ol\"\n  }, \"React finds another call to useState and because a Hook exists in the second position, once again, it just changes the current Hook variable and returns the array with the current state and the function to update it\")), mdx(\"h2\", {\n    \"id\": \"further-reading\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#further-reading\",\n    \"aria-label\": \"further reading permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Further Reading\"), mdx(\"ul\", null, mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 Official React documentation: \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://react.dev/reference/react/useState\"\n  }, \"useState\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://dev.to/johnstonlogan/react-hooks-barney-style-1hk7\"\n  }, \"useState() vs setState() - Strings, Objects, and Arrays\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDCC4 \", mdx(\"strong\", {\n    parentName: \"li\"\n  }, \"Advanced material\"), \" - includes concepts of future sections. \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://blog.logrocket.com/guide-choosing-right-react-state-management-solution/\"\n  }, \"A guide to choosing the right React state management solution\")), mdx(\"li\", {\n    parentName: \"ul\"\n  }, \"\\uD83D\\uDC69\\u200D\\uD83D\\uDCBB\\uD83E\\uDDD1\\u200D\\uD83D\\uDCBB Exercises: \", mdx(\"a\", {\n    parentName: \"li\",\n    \"href\": \"https://www.clientside.dev/blog/react-use-state-practice-exercises\"\n  }, \"Practice React's useState hook with 8 Interactive Exercises\"))), mdx(\"h2\", {\n    \"id\": \"notes\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#notes\",\n    \"aria-label\": \"notes permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Notes\"), mdx(\"p\", null, \"Most of the documentation provided in this section comes from this tutorial - \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://blog.logrocket.com/guide-usestate-react\"\n  }, \"useState in React: A complete guide\")));\n}\n;\nMDXContent.isMDXComponent = true;","headings":[{"depth":2,"value":"Introduction to React Hooks"},{"depth":3,"value":"What are React Hooks?"},{"depth":3,"value":"State Hooks"},{"depth":2,"value":"Goals"},{"depth":2,"value":"Basic tools"},{"depth":2,"value":"Contents"},{"depth":3,"value":"UseState"},{"depth":3,"value":"What can useState hold?"},{"depth":3,"value":"Updating objects and arrays in useState"},{"depth":3,"value":"What does the React.useState do?"},{"depth":3,"value":"Declaring state, update state, state variable"},{"depth":4,"value":"Exercises* 👩‍💻🧑‍💻:"},{"depth":4,"value":"Additional material on these subjects:"},{"depth":3,"value":"Rules for using useState"},{"depth":2,"value":"Further Reading"},{"depth":2,"value":"Notes"}]}},"pageContext":{"slug":"/react-js/states/","prev":{"label":"Introduction","link":"/react-js/introduction"},"next":{"label":"Events & forms","link":"/react-js/events-forms"},"repositoryEditUrl":"https://github.com/rocketseat/gatsby-themes/tree/main/examples/gatsby-theme-docs/src/docs/react-js/states.mdx","repositoryProvider":"GitHub"}},"staticQueryHashes":["1954253342","2328931024","2501019404","973074209"]}