Skip to main content

Posts

Showing posts with the label useState

Building a Simple Todo App with Next.js and Context API

Next.js offers an efficient way to build server-side rendered React applications with improved performance and SEO benefits. Combining it with the Context API allows you to manage global state effectively, enhancing the user experience. In this blog post, we'll guide you through creating a simple todo app using Next.js and Context API, demonstrating the key functionalities and providing code examples. Prerequisites Before we start, ensure you have the following prerequisites: Basic understanding of HTML, CSS, and JavaScript Familiarity with React JS concepts Node.js and npm installed on your system Project Setup Create a new Next.js project using npx: npx create-next-app todo-app cd todo-app Install the necessary dependencies: npm install Setting Up Context API Create a new file named TodoContext.js in the src directory. This file will house the context provider and consumer components. Inside TodoContext.js , define the initial state and functions for interact...

Most Commonly Used React Hooks in ReactJS

React hooks allow you to use state and other React features without writing class components. This makes it easier to write React components that are both concise and performant. In this blog post, we will discuss some of the most commonly used React hooks, along with examples of how to use them. 1. useState The useState hook is used to manage state in a React component. State is a piece of data that can be used to track the current state of a component. import React, { useState } from 'react'; const MyComponent = () => { const [count, setCount] = useState(0); const handleClick = () => { setCount((prev)=>prev + 1); }; return ( <div> <p>Count: {count}</p> <button onClick={handleClick}>Increment</button> </div> ); }; export default MyComponent; 2. useEffect The useEffect hook is used to perform side effects in a React component. Side effects are actions that are performed outside of the rende...

Mimicking React State in Redux Toolkit with Objects and Hooks

To mimic React state in Redux Toolkit, you can use the createSlice function with the initialState and reducers options. The initialState option defines the initial state of your slice, and the reducers option defines how the state should be updated in response to actions. Here's an example of how to mimic a React state object that manages a list of items: import { createSlice } from "@reduxjs/toolkit"; const initialState = { items: {}, }; const itemsSlice = createSlice({ name: "items", initialState, reducers: { addItem: (state, action) => { state.items[action.payload.id] = action.payload; }, updateItem: (state, action) => { state.items[action.payload.id] = action.payload; }, removeItem: (state, action) => { delete state.items[action.payload.id]; }, }, }); export const { addItem, updateItem, removeItem } = itemsSlice.actions; export default itemsSlice.reducer; This slice defines an initial state wit...

Topics

Show more