DEV Community

Cover image for Day 4: React State and Hooks
Dipak Ahirav
Dipak Ahirav

Posted on • Updated on

Day 4: React State and Hooks

๐Ÿš€ Check Out My YouTube Channel! ๐Ÿš€

Hi everyone! If you enjoy my content here on Dev.to, please consider subscribing to my YouTube channel devDive with Dipak. I post practical full-stack development videos that complement my blog posts. Your support means a lot!

Welcome to Day 4 of our React.js learning journey Today, we'll dive into the concept of state in React and explore the powerful hooks that help us manage it.

Understanding State in React

In React, state refers to the internal data of a component that determines its behavior and rendering. State can be anything from user input, API responses, or any other data that needs to be tracked and updated within a component.

State is a crucial concept in React, as it allows components to be dynamic and interactive. When the state of a component changes, React will automatically re-render the component and its children to reflect the new state.

Introducing Hooks

Hooks are a relatively new feature in React (introduced in version 16.8) that allow you to use state and other React features in functional components. Prior to hooks, state management was primarily done in class components, which can be more complex and verbose.

The most commonly used hook is the useState hook, which allows you to add state to functional components.

import { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

In this example, the useState hook is used to create a state variable count and a function setCount to update it. The initial value of count is set to 0.

Other Useful Hooks

While useState is the most fundamental hook, React provides several other hooks that can help you manage different aspects of your components:

  • useEffect: Allows you to perform side effects in functional components, such as fetching data or setting up event listeners.
  • useRef: Provides a way to create mutable references to DOM elements or values that persist across component re-renders.
  • useContext: Allows you to access the context (a way to pass data through the component tree without having to pass props down manually at every level) in functional components.
  • useReducer: Provides an alternative to useState for more complex state management scenarios, using a reducer function.

Example Components

Here are some example components that demonstrate the usage of these hooks:

// Example component using the useState hook
function Counter() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
}

// Example component using the useEffect hook
function FetchData() {
  const [data, setData] = useState([]);

  useEffect(() => {
    // Fetch data from an API
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data));
  }, []);

  return (
    <div>
      <h2>Fetched Data</h2>
      <ul>
        {data.map(item => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
    </div>
  );
}

// Example component using the useContext hook
const ThemeContext = React.createContext('light');

function ContextExample() {
  return (
    <ThemeContext.Provider value="dark">
      <ThemedButton />
    </ThemeContext.Provider>
  );
}

function ThemedButton() {
  const theme = React.useContext(ThemeContext);

  return (
    <button style={{ backgroundColor: theme === 'dark' ? '#333' : '#fff', color: theme === 'dark' ? '#fff' : '#333' }}>
      Themed Button
    </button>
  );
}

// Example component using the useReducer hook
function reducer(state, action) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

function CounterWithReducer() {
  const [state, dispatch] = React.useReducer(reducer, { count: 0 });

  return (
    <div>
      <p>Count: {state.count}</p>
      <button onClick={() => dispatch({ type: 'increment' })}>Increment</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>Decrement</button>
    </div>
  );
}

function App() {
  return (
    <div>
      <Counter />
      <FetchData />
      <ContextExample />
      <CounterWithReducer />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

In this example, we have:

  1. Counter: A component that uses the useState hook to manage the count state and update it when the button is clicked.
  2. FetchData: A component that uses the useEffect hook to fetch data from an API and update the state with the fetched data.
  3. ContextExample: A component that demonstrates the usage of the useContext hook to access a theme context and apply it to a button.
  4. CounterWithReducer: A component that uses the useReducer hook to manage the count state using a reducer function.

Conclusion

In this blog post, you've learned about the importance of state in React and how hooks, particularly the useState hook, can help you manage state in functional components. Hooks have revolutionized the way we write React code, making it more concise, readable, and reusable.

As you continue your React.js learning journey, be sure to explore the other hooks mentioned and understand how they can help you build more powerful and dynamic applications.

Stay tuned for Day 5, where we'll cover conditional rendering and lists in React. I hope this blog post has helped you grasp the concepts of state and hooks in React. Remember to practice using the useState hook and experiment with other hooks to solidify your understanding. Good luck with your React.js learning journey!

Top comments (1)

Collapse
 
dipakahirav profile image
Dipak Ahirav

Next part -> Day - 5