Article

React Dev Global State.

April 14, 2023 - Rebaz Omar

The first part is the MyApp component, which is the top-level component of the application. It imports GlobalStateProvider from the "rebaz-dev-global-state" package and wraps its child components with it. The stateNames prop of GlobalStateProvider takes an array of state names that will be used in the application. In this case, it is [nameState], which presumably refers to the name of a single global state.

What is your benefit with this package?

  • Simpler state management: Using global state can simplify state management in a React application, as it removes the need to pass data between multiple layers of components via props. This can make the code easier to understand and maintain.
  • Centralized data source: With global state, data is stored in a centralized location and can be accessed and modified from anywhere in the application. This can make it easier to keep track of data and ensure that it remains consistent.
  • Easy to share data between components: Global state makes it easy to share data between different components, even if they are not directly related. This can make it easier to create reusable components and to keep the code modular.
  • Performance optimizations: The "rebaz-dev-global-state" package includes optimizations such as memoization, which can help to improve performance by reducing unnecessary re-renders.
  • Flexibility: The package provides a simple API for managing global state, but it is also flexible enough to allow for more advanced use cases. For example, it supports asynchronous data fetching and updating, as well as middleware for intercepting and modifying state changes.

How to start?

try install the rebaz-dev-global-state

"you must use React 18.2.0 or follow this steps change the package React version to (your version) by deleting node_modules from and replacing react from package.json then try `npm i` and `npm run build` from rebaz-dev-global-state folder."

if you are ready try install

$ npm install rebaz-dev-global-state --save

or

$ yarn add rebaz-dev-global-state --save

The first part is the MyApp component, which is the top-level component of the application. It imports GlobalStateProvider from the "rebaz-dev-global-state" package and wraps its child components with it. The stateNames prop of GlobalStateProvider takes an array of state names that will be used in the application. In this case, it is [nameState], which presumably refers to the name of a single global state.

import React from 'react';

import React from "react";
import { GlobalStateProvider } from "rebaz-dev-global-state";

function MyApp({ Component, pageProps }) {
return (
  <GlobalStateProvider stateNames={[nameState]}>
    <Component {...pageProps} />
  </GlobalStateProvider>
);
}
export default MyApp;

The second part of the code exports a component called App that uses the global state. It imports useContext and GlobalStateContext from the same "rebaz-dev-global-state" package to access the global state. state and setData are extracted from GlobalStateContext using useContext. state contains the current values of all the global states, and setData is a function to update a specific global state.

import React from 'react';
import React, { useContext, useEffect } from "react"

import { GlobalStateContext } from "rebaz-dev-global-state"

export default function App() {
const { state, setData } = useContext(GlobalStateContext)

const handleUpdateState = () => {
  setData(nameState,value)
}

const getState = () => {
  console.log(state?.nameState)
}

return (
  <>
    <div>APP</div>
  </>
)
}

congratulations 🎉🎉🎉