context-api

How to use Context API to pass down a state while using a Router in React JS

自作多情 提交于 2021-02-11 14:11:04
问题 I have a Context API file setup which has a state and a function which fetches data from an API and sets the state, and i want to pass the state down to my other components. In my App.js, I am using React-Router to specify the routes. How do i pass the state down to these components using Context API, whilst using React-Router. My ApiContext.js file looks like this : import React, {useState, createContext } from 'react'; export const ApiContext = createContext(); export const ApiProvider =

Convert React Context API Class to function of Hook

*爱你&永不变心* 提交于 2021-02-10 14:19:49
问题 How to change this class base Context API to reach Hook without changing other components that already consumed it? I am new to react and spend all night and I got stuck. The actual code is more than this but I'm trying to remove some of the code for simplicity purposes: import React, { createContext, Component } from 'react' const MainContext = createContext(); class MainContextProvider extends Component { state = { isLogin : false, loginData : [], spinner : false } handleUserLogin = (res) =

dispatch is not accessible from useContext

橙三吉。 提交于 2021-01-28 08:23:28
问题 i have simple store import React, { createContext, useReducer } from "react"; import Reducer from "./UserReducer"; const initialState = { user: {}, error: null }; const Store = ({ children }) => { const [state, dispatch] = useReducer(Reducer, initialState); return ( <Context.Provider value={[state, dispatch]}> {children} </Context.Provider> ); }; export const Context = createContext(initialState); export default Store; i have wrapped my app with it like <Store> <ThemeProvider theme={Theme}>

React Context Api vs Local Storage

Deadly 提交于 2020-08-09 09:14:08
问题 I have some general questions which are bothering me regarding the context API and local storage. When to use local storage?, when to use the Context API and when would I use both? I know that to persist data after the refresh I need something like local storage or session storage, so do I ditch the context API entirely and just store everything in the local storage? this way I can not only store data but also keep it on refresh? some insight would be really helpful. What are some pros and

Losing dropdown select options upon filtering using react context api

僤鯓⒐⒋嵵緔 提交于 2020-05-16 03:01:29
问题 I just started learning React 2 weeks ago so I am still finding some concepts difficult. I went from building a CRA to converting it to NextJS and opting for using context API with hooks for state management. I am working on filtering right now for a list of articles using a dropdown. When I select a certain type of article, the list filters correctly, but as I go back to the dropdown, only the original option is there along with the current option. When I click on the original option, all

Losing dropdown select options upon filtering using react context api

浪尽此生 提交于 2020-05-16 03:00:08
问题 I just started learning React 2 weeks ago so I am still finding some concepts difficult. I went from building a CRA to converting it to NextJS and opting for using context API with hooks for state management. I am working on filtering right now for a list of articles using a dropdown. When I select a certain type of article, the list filters correctly, but as I go back to the dropdown, only the original option is there along with the current option. When I click on the original option, all