Lädt...

🔧 How to use React Context API with npm library


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Hi. I am writing an admin project package with React. there are Auth operations in this package. I can access the conxtex by saying useAuth in the components in the package, but the context data does not come properly in the project using the package. I think there is a problem here due to packaging. Exactly how can I solve it, I don't want to take the Provider out and provide it in the components where the package is used.

AuthContext.ts

const initialAuthState = {
  user: null, // or any initial state you want
  isAuthenticated: false,
};

// Create a context with the initial state
export const AuthContext = createContext({
  authState: initialAuthState,
  setAuthState: () => {}
});

// AuthProvider component to provide auth state to its children
export const AuthProvider = ({ children }) => {
  const [authState, setAuthState] = useState(initialAuthState);

  useEffect(() => {
    // Example of updating state, replace this with your actual logic
    const fetchAuthState = async () => {
      // Simulate a fetch or any async operation to get auth data
      const newState = {
        user: { name: 'John Doe' },
        isAuthenticated: true,
      };
      setAuthState(newState);
    };

    fetchAuthState();
  }, []);

  return (
    <AuthContext.Provider value={{ authState, setAuthState }}>
      {children}
    </AuthContext.Provider>
  );
};

// Custom hook to use the auth context
export const useAuth = () => {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error("useAuth must be used within an AuthProvider");
  }
  return context;
};


//Container.tsx
export const Container = ({ children }) => {
  return (
    <AuthProvider>
      {children}
      <ExamplePage/>
    </AuthProvider>
  );
};

//ExamplePage.tsx
export const ExamplePage = () => {
  const authData = useAuth();

  //the data in this log is coming in correctly. 
  console.log('authData:', data);
  return (
    <div>
      example page
    </div>
  );
};


//app.tsx (other project)
import {Container} from 'my-package'

export const App = () => {
  return (
    <Container>
      <ListPage/>
    </AuthProvider>
  );
};


//listPage.tsx
import {useAuth} from 'my-package'

export const ListPage = () => {
  const authData = useAuth();


  console.log('authData:', data);   
  return (
    <div>
        List page
    </div>
  );
};

...

🔧 How to use React Context API with npm library


📈 41.82 Punkte
🔧 Programmierung

🔧 How to use the Context API, What is Context API?


📈 33.94 Punkte
🔧 Programmierung

🔧 Redux-Toolkit vs React Context API: Mastering State Management in React


📈 25.92 Punkte
🔧 Programmierung

🔧 State Management in React: When to Use Context API vs. Redux


📈 24.24 Punkte
🔧 Programmierung

🔧 How to Use the React Context API in Your Projects


📈 24.24 Punkte
🔧 Programmierung

🔧 How to Use React's Context API – Tutorial with Examples


📈 24.24 Punkte
🔧 Programmierung

🔧 Context API vs. Redux: When and Why to Use Them in React


📈 24.24 Punkte
🔧 Programmierung

🔧 Understanding The Use Of Context API In React JS


📈 24.24 Punkte
🔧 Programmierung

🔧 How to Use the React Context API in Your Projects


📈 24.24 Punkte
🔧 Programmierung

🔧 React & TypeScript: How to use Context API and useReducer with Firestore Database?


📈 24.24 Punkte
🔧 Programmierung

🔧 🛰️ SpaceX Kotlin API Library – Open-Source Kotlin Library for SpaceX API Integration


📈 23.37 Punkte
🔧 Programmierung

🔧 Publish a Typescript React library to NPM in a monorepo


📈 22.99 Punkte
🔧 Programmierung

🔧 Exploring the React Typing Effect NPM Library


📈 22.99 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 21.63 Punkte
🔧 Programmierung

🔧 This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


📈 21.63 Punkte
🔧 Programmierung

🔧 'Create-react-tailwindcss ' an npm package to setup react-tailwindcss configuration


📈 21.49 Punkte
🔧 Programmierung

🔧 npm i vs npm ci


📈 21.35 Punkte
🔧 Programmierung

🔧 NPM Config: customising how npm works


📈 21.35 Punkte
🔧 Programmierung

🔧 NPM Config: customising how npm works


📈 21.35 Punkte
🔧 Programmierung

🔧 npm toggle-beautify | my first npm package


📈 21.35 Punkte
🔧 Programmierung

🔧 Solving the NPM "Can't Find Path `npm`" Error on Windows


📈 21.35 Punkte
🔧 Programmierung

🔧 Simplifying Your Workflow: npm run vs npm --run


📈 21.35 Punkte
🔧 Programmierung

🕵️ Medium CVE-2020-7614: Npm-programmatic project Npm-programmatic


📈 21.35 Punkte
🕵️ Sicherheitslücken

🔧 Fixing High and Critical Vulnerabilities in npm Using npm audit


📈 21.35 Punkte
🔧 Programmierung

🔧 Fixing High and Critical Vulnerabilities in npm Using npm audit


📈 21.35 Punkte
🔧 Programmierung

🔧 Icarus and the npm Updates: How Version Control Rescued My npm Upgrade Nightmare


📈 21.35 Punkte
🔧 Programmierung

matomo