How to keep a value with react context? - reactjs

Here are 3 basic components searchBar which refers to the search bar form and, the searchPage component which displays the search results, and of course, the app component which contains them all.
mechanism:
the user submits an input in the searchBar component, the
handleSubmit function gets fired, which changes the state of
setSearchedProducts to the input value, by useContext AND
getting pushed to the ("/SearchPage") by history.push() .
import {useState, useContext } from "react";
import { useHistory } from "react-router-dom";
import { LocaleContext } from "../../../LocaleContext";
const SearchBar = () => {
const history = useHistory();
const {setSearchedTerm} = useContext(LocaleContext);
const handleSubmit = (SearchTerm) => {
setSearchedProducts(SearchTerm)
history.push("/SearchPage");
}
return (
<form>
<input onSubmit={(e) => handleSubmit(e.target.value)}>
</input>
<button type="submit">Submit</button>
</form>
)
}
export default SearchBar
the value gets sent to the app component by react context and
the state gets set to the value while still pushing to the
("/searchPage").
import { useState, useMemo } from "react";
import { searchBar, searchPage } from "./components";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import {LocaleContext} from "./LocaleContext"
const App = () => {
const [searchedTerm, setSearchedTerm] = useState("");
const providerValue = useMemo(() => ({searchedTerm, setSearchedTerm}),
[searchedTerm, setSearchedTerm])
return (
<Router>
<LocaleContext.Provider value={providerValue}>
<searchBar />
<Switch>
<Route exact path="/SearchPage">
<SearchPage />
</Route>
</Switch>
</LocaleContext.Provider>
</Router>
);
}
export default (App);
displaying the searchPage component, which gets the state value
by using useContext, and with useEffect, the fetchProducts()
function gets fired, that fetches a set of products based on the
state value.
import {useState, useEffect, useContext} from 'react';
import { LocaleContext } from "../../LocaleContext";
const SearchPage = ({}) => {
const [products, setProducts] = useState([]);
const {searchedTerm} = useContext(LocaleContext);
const fetchProducts = (term) => {
setLoading(true);
const url = new URL(
"https://example/products"
);
let params = {
"query": term
};
Object.keys(params)
.forEach(key => url.searchParams.append(key, params[key]));
let headers = {
"Accept": "application/json",
"Content-Type": "application/json",
};
fetch(url, {
method: "GET",
headers: headers,
})
.then(response => response.json())
.then(json => {
setProducts(json);
});
}
useEffect(() => {
fetchProducts(searchedProducts)
}, [])
return (
{
products.map(product => (
<div>
{product.name}
</div>
))
}
)
}
export default SearchPage
Issues:
when the router changes to the ("/searchPage") component state value get lost, meaning it returns to "" value. ?
lesser problem, if the user sends an empty string (" "), and the API needs a value or it will give an error, what is the solution to that?
is there a way of keeping the value after reloading the page?
import {createContext} from "react";
export const LocaleContext = createContext(null);
this is the localeContext component if needed.

you have to add e.preventDefault() in your onSubmit handler. Otherwise you're getting a page reload which causes a state loss.

I noticed "setSearchedProducts" & "setSearchedTerm" should be the same in your code below. This might be your issue!
const SearchBar = () => {
...
const {setSearchedTerm} = useContext(LocaleContext);
const handleSubmit = (SearchTerm) => {
setSearchedProducts(SearchTerm)
...
}

Related

export 'withRouter' (imported as 'withRouter') was not found in 'react-router-dom'

I am totally a beginner in React and while practising I ran into this issue. Through searching, I found out that 'withRouter' is not supported anymore by 'react-router-dom v6'. But I can't figure out how to change my code compatibly to v6. Does anyone know how to change this code instead of using 'withRouter'? Thanks in advance!
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { readPost, unloadPost } from '../../modules/post';
import PostViewer from '../../components/post/PostViewer';
const PostViewerContainer = ({ match }) => {
// 처음 마운트될 때 포스트 읽기 API요청
const { postId } = match.params;
const dispatch = useDispatch();
const { post, error, loading } = useSelector(({ post, loading }) => ({
post: post.post,
error: post.error,
loading: loading['post/READ_POST']
}));
useEffect(() => {
dispatch(readPost(postId));
// 언마운트될 때 리덕스에서 포스트 데이터 없애기
return () => {
dispatch(unloadPost());
};
}, [dispatch, postId]);
return <PostViewer post={post} loading={loading} error={error} />;
};
export default withRouter(PostViewerContainer);
enter image description here
That is correct, the withRouter Higher Order Component (HOC) was removed in react-router-dom#6.
Since PostViewerContainer is a function component, just use the React hooks directly. There's no need really for the withRouter HOC. In this case it's the useParams hook you need to import and use.
Example:
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useParams } from 'react-router-dom'; // <-- import useParams hook
import { readPost, unloadPost } from '../../modules/post';
import PostViewer from '../../components/post/PostViewer';
const PostViewerContainer = () => { // <-- remove match prop
// 처음 마운트될 때 포스트 읽기 API요청
const { postId } = useParams(); // <-- call hook and destructure param
const dispatch = useDispatch();
const { post, error, loading } = useSelector(({ post, loading }) => ({
post: post.post,
error: post.error,
loading: loading['post/READ_POST']
}));
useEffect(() => {
dispatch(readPost(postId));
// 언마운트될 때 리덕스에서 포스트 데이터 없애기
return () => {
dispatch(unloadPost());
};
}, [dispatch, postId]);
return <PostViewer post={post} loading={loading} error={error} />;
};
For reference, if you needed to still use an HOC for class based components you'd need to either convert them to function components or create a custom withRouter HOC.
Example:
import { useLocation, useNavigate, useParams } from 'react-router-dom';
const withRouter = Component => props => {
const location = useLocation();
const navigate = useNavigate();
const params = useParams();
return (
<Component
{...props}
location={location}
navigate={navigate}
params={params}
/>
);
};
export default withRouter;

Why cannot I grab items from api/context?(react question)

I am trying to create a simple react app for lending phones with this api.
I am trying to grab the mobiles with context api like this:
import React, { useState, useEffect, createContext
} from 'react';
import axios from 'axios';
export const MobileContext = createContext({
mobiles: [],
setMobiles: () => {},
updateMobiles: () => {},
});
export default function MobileProvider(props) {
const [mobiles, setMobiles] = useState([]);
const updateMobiles = (id) => {
axios
.get('https://js-test-api.etnetera.cz/api/v1/phones')
.then((res) => setMobiles(res.data));
};
useEffect(() => {
axios
.get('https://js-test-api.etnetera.cz/api/v1/phones')
.then((res) => setMobiles(res.data));
}, [] );
return (
<MobileContext.Provider value={{ mobiles, setMobiles, updateMobiles }}>
{props.children}
</MobileContext.Provider>
);
}
and reuse them at the main page after logging in
import React from 'react'
import { MobileContext } from './MobileContext';
import { useContext } from 'react';
import Mobile from './Mobile';
import Navbar from './Navbar';
function MobileList() {
const { mobiles } = useContext(MobileContext);
return (
<div>
<Navbar/>
{mobiles.map((item) => (
<Mobile
vendor={item.vendor}
/>
))}
</div>
)
}
export default MobileList
and this is the single mobile component
import React from 'react'
function Mobile(props) {
return (
<div>
<p>{props.vendor}</p>
<p> ssssssssssss</p>
</div>
)
}
export default Mobile
after the correct logging in, it should display both the text and the vendor for each mobile but it isnt displaying anything besides the navbar
this would probably mean, that I am not getting the mobiles from the api in the first place, but I am not sure why is that. The auth token could also be the reason why I am not able to access the phones,never used it before.
Anyway, this is the full code and I would apreciate any help
login.js
import React from 'react'
import axios from 'axios';
import { useState } from 'react';
import { useHistory } from "react-router-dom";
function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
let history = useHistory()
const onSubmit = (e) => {
e.preventDefault();
const getIn = {
"login":email,
"password":password,
};
axios
.post('https://js-test-api.etnetera.cz/api/v1/login', getIn,
{
headers: {
'content-type': 'application/json',
}
}).then((res) => {
console.log(res.data);
history.push("/phones");
})
.catch((error) => console.log(error));
};
return (
<div>
<form >
<label>email</label> <input value={email}
onChange={(e) => setEmail(e.target.value)} type="text"/>
<label>password</label> <input type="text" value={password}
onChange={(e) => setPassword(e.target.value)}/>
<button onClick={onSubmit}>login</button>
</form>
</div>
)
}
export default Login
As you said, it's the get api expecting an auth token. You need to first login using the login endpoint and get the token from the login response. Post that you can pass that auth token in each get request in the header.
You can update your context file like so :-
import React, { useState, useEffect, createContext
} from 'react';
import axios from 'axios';
export const MobileContext = createContext({
login:()=>{},
mobiles: [],
setMobiles: () => {},
updateMobiles: () => {},
});
export default function MobileProvider(props) {
const [mobiles, setMobiles] = useState([]);
const [token,setToken] = useState(null);
const login = (username,password) =>{
// do the axios post thing - take ref from docs you shared for request body
// get the token from the response and you can set it in the state
setToken(token);
}
const updateMobiles = (id) => {
//Update this get request with proper header value using token state as well.
axios
.get('https://js-test-api.etnetera.cz/api/v1/phones')
.then((res) => setMobiles(res.data));
};
useEffect(() => {
//Update this get request with proper header value using token state as well.
axios
.get('https://js-test-api.etnetera.cz/api/v1/phones')
.then((res) => setMobiles(res.data));
}, [] );
return (
<MobileContext.Provider value={{ login,mobiles, setMobiles, updateMobiles }}>
{props.children}
</MobileContext.Provider>
);
}
Note - How you wan't to use that login function is upto you but generally its through form submission. In your case I think it's an auto login inside an useEffect, so don't hardcode username and password in the UI. You can use environment variables for the same.

Is there a way I can pass a property between components, without using the props object?

I have a component that displays a list of cafes. Within this component ( CafeList.jsx), an axios request is made which returns a list of cafes, which is the mapped over and rendered to the browser.
I'd like users to be able to click on a cafe, then be directed to a page with specific information about that particular cafe (at this stage it's CafeReview.jsx).
I need to pass the cafe ID (_id) from CafeList to CafeReviews, so that I can use it in an axios request that brings back specific data about the cafe that was clicked on. Any suggestions? Also, do I have the right general approach?
Components
CafeList.jsx
import React, {useState, useEffect} from 'react'
import axios from 'axios'
import {Link} from 'react-router-dom'
const CafeList = () => {
const [cafes, setCafe] = useState([])
useEffect(() => {
axios.get('/api/all-cafes')
.then(cafe => {
setCafe(cafe.data)
})
.catch(err => {
console.log(err)
})
},[])
return(
<div className = 'cafe-container-container'>
<h2>Cafes</h2>
{
cafes.map(cafe =>{
const {cafeName,photoURL,_id} = cafe
return (
<Link to = {`/cafe-reviews/${_id}`} style={{ textDecoration: 'none' }} >
<div className = 'cafe-container'>
<h2>{cafeName}</h2>
<img src = {photoURL}></img>
</div>
</Link>
)
})
}
</div>
)
}
export default CafeList
CafeReviews.jsx
import React,{useState, useEffect} from 'react'
import axios from 'axios'
const CafeReviews = () => {
const [cafe,setCafe] = useState([])
useEffect(() => {
axios.get('/api/cafe/:id')
.then(result => {
setCafe(result.data)
})
},[])
return(
<div>
{
cafe.map(item => {
return (
<h2>{item.cafeName}</h2>
)
})
}
</div>
)
}
export default CafeReviews
Routes and data models
GET cafe by id:
app.get('/api/cafe/:id', (req,res) => {
const id =req.params.id
Cafe.findById(id)
.then(result => {
res.send(result)
})
.catch(err => {
console.log(err)
})
})
Cafe Model:
const mongoose = require('mongoose')
const Schema = mongoose.Schema
const cafeSchema = new Schema({
cafeName:String,
photoURL:String,
}, {timestamps:true})
const Cafe = mongoose.model('cafes', cafeSchema)
module.exports = Cafe
Router:
import React from 'react'
import AddReview from './components/AddReview'
import Main from './components/Main'
import AllReviews from './components/AllReviews'
import CafeReviews from './components/CafeReviews'
import './styles.css'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom'
const App = () => {
return(
<Router>
<div>
<Switch>
<Route path ='/' exact component = {Main}/>
<Route path ='/add-review' component = {AddReview}/>
<Route path ='/all-reviews' component = {AllReviews}/>
<Route path ='/cafe-reviews/:id' component = {CafeReviews}/>
</Switch>
</div>
</Router>
)
}
export default App;
Since the CafeReviews component is directly rendered by a <Route/>, by default is has all the react router props passed to it. From there you can access the params, which will contain the :id of that specific cafe in the URL. So try something like this:
const CafeReviews = ({ match }) => {
const [cafe,setCafe] = useState([])
useEffect(() => {
axios.get(`/api/cafe/${match.params.id}`)
.then(result => {
setCafe(result.data)
})
},[])
Haven't tested, might need to check the docs react-router-dom to see if that's correct shape of object and such, but in general that's how to access the params inside the component

How to use context with hooks for authentication?

I'm trying to use context for handling pieces of authentication in my app. I was running into issues because I was trying to call useContext outside of my Context.Provider, so I moved the logic to a child component of the provider.
Now I'm getting an error message TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) where I'm calling useContext in the child component. Is the issue really with getting the values from the context or something else?
In app.js
import AuthContextProvider from "./components/context/authContext";
import RegisterRoutes from "./components/routing/registerRoutes";
function App() {
return (
<AuthContextProvider>
<Route
exact
path="/register"
render={(props) => (
<RegisterRoutes {...props} />
)}
/>
</AuthContextProvider>
)
}
In my authContext.js
import React, { useState, useEffect, createContext } from "react";
export const AuthContext = createContext();
const AuthContextProvider = (props) => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const setAuth = (boolean) => {
setIsAuthenticated(boolean);
};
//Auth API logic here//
const apiOptions = {
url: "users/is-verified",
method: "GET",
headers: {
token: localStorage.token,
},
};
async function isAuth() {
axios(apiOptions)
.then((response) => {
const resData = response.data;
resData === true ? setIsAuthenticated(true) : setIsAuthenticated(false);
})
.catch((error) => {
console.log(error.response);
});
}
useEffect(() => {
isAuth();
}, []);
return (
<AuthContext.Provider
value={[isAuthenticated, setIsAuthenticated, setAuth]}
>
{props.children}
</AuthContext.Provider>
);
};
export default AuthContextProvider;
In my registerRoutes.js
import React, { useContext } from "react";
import { Redirect } from "react-router-dom";
import Register from "../pages/register";
import AuthContext from "../context/authContext";
function RegisterRoutes(props) {
const [isAuthenticated, setAuth] = useContext(AuthContext);
return !isAuthenticated ? (
<Register {...props} setAuth={setAuth} />
) : (
<Redirect to="/login" />
);
}
export default RegisterRoutes;
As the error says, the Context.Provider in authContext.js value is not iterable:
<AuthContext.Provider value={[isAuthenticated, setIsAuthenticated, setAuth]}>
The value passed to the provider needs to be an iterable value, in this case, a valid JSON object, instead of the array that you have provided. so, we change it to:
<AuthContext.Provider value={{isAuthenticated, setIsAuthenticated, setAuth}}>
Then you change the reference in registerRoutes.js to correctly consume the new structure:
const [isAuthenticated, setAuth] = useContext(AuthContext);
becomes
const { isAuthenticated, setAuth } = useContext(AuthContext);
Voila! Your Context.Provider value is iterable and you can consume it in your application.
I think this will help you. My solution for accessing data in the context is creating a custom hook.
//localState.js
import { createContext, useState, useContext } from 'react'
const LocalStateContext = createContext()
const LocalStateProvider = LocalStateContext.Provider
function LocalState({children}) {
const [someState, setSomeState] = useState('')
const defaultValues = {
someState, setSomeState
}
return <LocalStateProvider value={defaultValues}>
{children}
</LocalStateProvider>
}
function useLocalState() {
const all = useContext(LocalStateContext)
return all
}
export {LocalState, LocalStateContext, useLocalState}
With this code you can wrap your whole app in the LocalState component and access context values by using the new useLocalState hook. For example
import { useLocalState} from './localstate'
const SomeComponent = () => {
const { someState } = useLocalState()
return (
///...whatever you want
)
}
export default SomeComponent
I think your issue may be that you have put your default values in an array inside of the value object.

Does history.push supposed to render the component associated with the route?

So I'm creating a loader component that requests data to a server, then based on the response - then the loader page redirects/reroutes them to other pages.
But for some reason history.push changes the route but does not render the component and remains with the <LoaderPage> component. Not sure what am I missing. So kindly help.
I have wrapped all pages with the <LoaderPage> component as my goal is when every time a user visits any route the <LoaderPage> component renders first then it does its job then redirects/reroutes users to other pages.
loader.tsx
import React from 'react';
import { useHistory } from 'react-router-dom'
import { AnimatedLodingScreen } from './loaders/AnimatedLodingScreen'
type loaderProps = {
children: React.ReactNode;
};
export const LoaderPage = ({ children }:loaderProps) => {
const history = useHistory();
React.useEffect(() => {
const session = http.get('/some-route');
session.then((result) => {
if(result.authenticated) {
history.push('home'); // when user is logged
}
history.push('/login'); // not authenticated
}
}, [])
return (
<AnimatedLodingScreen/>
);
}
app.tsx
import React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { observer } from 'mobx-react';
import { LoaderPage } from 'pages/loaders/LoaderPage';
import { SomeComponent1, SomeComponent2 } from 'pages/index'
export const App: React.FC = observer(() => {
return (
<BrowserRouter>
<LoaderPage>
<Route path='/home' exact component={SomeComponent1}/>
<Route path='/login' exact component={SomeComponent2}/>
// and so on... I have alot of routes in fact these routes are looped via .map and
// type-checked i just put it like this for simplicity
</LoaderPage>
</BrowserRouter>
);
});
index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import { App } from 'app/app';
ReactDOM.render(<App/>, document.getElementById('root'));
The children props taken by your LoaderPage component isn't used for render anywhere within it, and thus nothing is rendered.
export const LoaderPage = ({ children }:loaderProps) => {
const history = useHistory();
React.useEffect(() => {
const session = http.get('/some-route');
session.then((result) => {
if(result.authenticated) {
history.push('home'); // when user is logged
}
history.push('/login'); // not authenticated
}
}, [])
return (
{children || <AnimatedLodingScreen/>}
);
}
You can use a state to save whether the data loading is completed or not and render children based on that
export const LoaderPage = ({ children }:loaderProps) => {
const history = useHistory();
const [isLoaded, setLoaded] = React.useState(false)
const [redirectPath, setRedirectPath] = React.useState('')
React.useEffect(() => {
const session = http.get('/some-route');
session.then((result) => {
if(result.authenticated) {
return setRedirectPath('/home') // when user is logged
}
setRedirectPath('/login') // not authenticated
}
}, [])
function redirectToPath() {
setLoaded(true);
history.push(redirectPath)
}
if(isLoaded) { return <>{children}</> }
return <AnimatedLodingScreen onAnimationEnd={redirectToPath} /> // onAnimationEnd is the function passed as prop to the component that should be invoked on animation ends
}

Resources