UseParams hook in React keeps returning undefined - reactjs

I'm just making a practice recipe app that uses a GET request to show recipes which I'm mapping through into their own individual Recipe components so I can give each an edit & delete button. For some reason, I thought I setup useParams correctly as well as matched the dynamic :id with what I extracted into the useParams hook aka { id } but it keeps popping up undefined. Everything else setup with Redux has been working without errors.
Here is my App.js file:
import { Route, BrowserRouter, Switch } from "react-router-dom";
import Recipe from "./Recipe";
import Recipes from "./Recipes";
function App() {
return (
<div className="App">
<BrowserRouter>
<Switch>
<Route path='/recipes/:id' component={Recipe} />
<Route exact path='/recipes' component={Recipes} />
</Switch>
</BrowserRouter>
</div>
);
}
export default App;
and here is my Recipe.js file:
import { useDispatch } from 'react-redux';
import { deleteRecipe } from './actions';
import { Link, useParams } from 'react-router-dom';
function Recipe({ recipe }) {
const dispatch = useDispatch();
let { id } = useParams();
console.log('id:', id);
const handleDelete = (id) => {
dispatch(deleteRecipe(id));
}
return (
<div>
<Link to={`/recipes/${id}`}>
<h5>{recipe.title}</h5>
<button onClick={handleDelete}>Delete</button>
</Link>
</div>
)
}
export default Recipe

It looks like you are in the Recipe show method trying to Link to another Recipe show. You will need to use the Recipes component, with a Link that houses the dynamic/static id. Then you can call useParams to get that id.
Example here from previous answer.

Related

rendering component, after another distant component renders

In navigation menu app, down the component tree, there is a dropdown menu component DropdownMenu2, with menu items, which are <NavLinks> components. Every time an item is clicked, it points to one of the <Route>s in main App. Every <Route> is a page, containing Infofield component. So every time <NavLink> is clicked, Infofield is rendered.
My puzzle is: I need the HeaderLogo component be rendered, everytime Infofield is rendered (HeaderLogo contains animation). I failed when constructing useEffect hook in Infofield. That hook was intended to contain custom hook, producing a variable with changing state. That hook could be then lifted up to App, from there variable would be passed to HeaderLogo, inline to the key property. If that idea is legit, I'm experiencing difficulties with construction of custom hook inside of useEffect. Maybe (probably) there is a better way...
Apps most basic structure looks like this:
App
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import HeaderLogo from "./components/HeaderLogo";
import NaviMain from "./components/NaviMain";
import Info from "./pages/Info";
/...
import { UserContext } from "./components/sub-components/UserContext";
function App() {
return (
<Router>
<div className="App">
<HeaderLogo />
<NaviMain />
<Routes>
<Route path="/Info" element={<Info />} />
/...
</Routes>
</div>
</Router>
);
}
export default App;
NaviMain
import "./NaviMain.css";
import NaviMainButton from "./NaviMainButton";
import NaviMainButtonDrop2 from "./NaviMainButtonDrop";
const NaviMain = () => {
return (
<nav>
<ul>
<NaviMainButtonDrop2 />
</ul>
</nav>
)
}
export default NaviMain
NaviMainButtonDrop2
import DropdownMenu2 from "./DropdownMenu2";
const NaviMainButtonDrop2 = () => {
return (
<li>
<a>
title
</a>
<DropdownMenu2 />
</li>
)
}
export default NaviMainButtonDrop2
DropdownMenu2
import "./DropdownMenu.css"
import { NavLink } from "react-router-dom";
import { MenuItemContentSchool } from "./sub-components/MenuItemContentSchool"
const DropdownMenu2 = () => {
return (
<div className=dropdown-holder-us>
{/* here menu unfolds */}
{MenuItemContentSchool.map((item) => {
return (
<NavLink
to={item.link}
className={(navData) => (navData.isActive ? "d-content-us active-style" : 'd-content-us')}
key={item.id}
>
{item.title}
</NavLink>
)
})}
</div>
)
}
export default DropdownMenu2
Info (one of the <Route>'s )
import InfoField from "../components/InfoField"
const Info = () => {
return (
<section className="intro-index">
<InfoField text={"welcome"} />
</section>
)
}
export default Info
HeaderLogo
import "./HeaderLogo.css";
const HeaderLogo = () => {
return (
<header>
<h1 className="head-main">learning curve</h1>
</header>
)
}
export default HeaderLogo
From what I can gather you simply want to "rerun" an animation in the HeaderLogo component when the path changes. Import and use the useLocation hook and use the pathname value as a React key on the header element with the animation to want to run when it mounts. The idea here is that when the React key changes, React will remount that element.
Example:
import { useLocation } from "react-router-dom";
import "./HeaderLogo.css";
const HeaderLogo = () => {
const { pathname } = useLocation();
return (
<header>
<h1 key={pathname} className="head-main">
learning curve
</h1>
</header>
);
};
export default HeaderLogo;
This is a classic job for a global state. You can declare a boolean state, i.e showHeader, and add conditional rendering to the tag.
The global state variable showHeader will be changed each time you click on a dropdown item, and in the App functional component you should listen for a change in this variable. (For example, using Redux, you'll use useSelector(state=>state.showHeader) in App.
For an example, this is the App component with conditional rendering for the HeaderLogo. In order for this to be useable, you need to build a Redux store and reducer functions. Read the official Redux docs for more
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import { useSelector } from 'react-redux';
import HeaderLogo from "./components/HeaderLogo";
import NaviMain from "./components/NaviMain";
import Info from "./pages/Info";
/...
import { UserContext } from "./components/sub-components/UserContext";
function App() {
const showHeader = useSelector(state=>state.showHeader)
return (
<Router>
<div className="App">
{showHeader ? <HeaderLogo /> : null}
<NaviMain />
<Routes>
<Route path="/Info" element={<Info />} />
/...
</Routes>
</div>
</Router>
);
}
export default App;
</Router>

How to get route params in React functional component

I want to print url params(:uname ,:unumber) through Profile functional component. Here is my code.
My route in App.js
<Route path="/profile/:uname-:unumber" component={Profile} >
<Profile />
</Route>
I'm clicking profile url from Dashboard.js; this is my link
<Link to={{
pathname: `/profile/${userDetail.name}-${userDetail.u_number}`,
}} >view profile</Link>
on click on this link it is generating a url for eg: 'http://localhost:3000/profile-mynamehere-747484' and it is redirecting to profile component. But how to print to access the uname and unumber in profile component. I tried like this but not works
import React, {useState, useEffect, Component } from "react";
import axios from "axios";
import { useHistory,useParams } from "react-router-dom";
import Header from "../Header";
import {
BrowserRouter as Router,
Route,
Link,
Redirect,
withRouter
} from "react-router-dom";
const Profile = ({uname, unumber}) => {
console.log(uname); // here I want to print mynamehere from url, but not prints name
console.log(unumber);// here I want to print 747484 from url, but not prints unumber
return (
<div>
</div>
);
};
export default Profile;
use UseParams like this in profile component
const { uname,unumber } = useParams();
In Profile you should useParams to get pass the uname & unumber:
import { useHistory,useParams } from "react-router-dom";
const Profile = () => {
const {uname, unumber} = useParams()
console.log(uname);
console.log(unumber);
return (
<div>
</div>
);
};
In your profile component do the following:
import React, {useState, useEffect, Component } from "react";
import axios from "axios";
import { useHistory,useParams } from "react-router-dom";
import Header from "../Header";
const Profile = () => {
const {uname, unumber} = useParams()
console.log(uname-unumber)
return (
<div>
</div>
);
};
export default Profile;

Pass parameters with React Router

Im new to React and i have the following app: (index.js)
import ReactDOM from 'react-dom'
import {Component} from 'react';
import './style.css'
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import Header from './components/Header'
import Body from './components/Body'
import Footer from './components/Footer'
class App extends Component {
render() {
return (
<div id="layout">
<Header />
<Body />
<Footer />
</div>
);
}
}
ReactDOM.render( <App />, document.querySelector("#root"));
Each time I'm going to localhost:port Im redirected to this componenet,
I wanted to use Router and extract the data, for example:
localhost:port/instanceName=value will route me to same compontents with props value only if value is x or y, is that possible?
I suggest you to setup it this way. It is just a custom hook which extract all url parameters.
// use-query-string.js
import { useLocation } from 'react-router-dom';
export const useQueryString = () => {
return new URLSearchParams(useLocation().search);
};
Then you can call this in your component which gives you a list of query strings in the url. In fact every time you call it, it would give you a list of passed parameters.
const queryString = useQueryString();
And finally you can extract what you want. Simply by passing the name of the parameter you are looking for.
queryString.get('instanceName')
In your case it could be something like this.
import { useQueryString } from 'use-query-string';
class App extends Component {
const queryString = useQueryString();
useEffect(() => {
const value = queryString.get('instanceName');
if (value === 'x') {
// do this
} else if (value === 'y') {
// do that
}
}, [queryString])
render() {
return (
<div id="layout">
<Header />
<Body />
<Footer />
</div>
);
}
}
But keep in mind that your url should be like localhost:port?instanceName=value. Actually instead of / at the end you need to put ?. I am not sure if it works with / as well. You can test and tweak if it does not.

How to provide context from contextApi for a specific set of routes in nextjs and preserve state with routing through linking?

I am using contextApi with nextjs and I'm having some trouble when providing a context just for certain routes. I am able to make the context available for just a few routes, but when I transition from one to the other through linking, I end up losing the state of my application.
I have three files inside my pages folder:
index.tsx,
Dashboard/index.tsx and
SignIn/index.tsx.
If I import the provider inside the files Dashboard/index.tsx and SignIn/index.tsx and go from one page to the other by pressing a Link component from next/link, the whole state is set back to the initial state.
The content of the Dashboard/index.tsx file
import React from 'react';
import Dashboard from '../../app/views/Dashboard';
import { AuthProvider } from '../../contexts/auth';
const Index: React.FC = () => (
<AuthProvider>
<Dashboard />
</AuthProvider>
);
export default Index;
This is the contend of the SignIn/index.tsx file:
import React from 'react';
import SignIn from '../../app/views/SignIn';
import { AuthProvider } from '../../contexts/auth';
const Index: React.FC = () => (
<AuthProvider>
<SignIn />
</AuthProvider>
);
export default Index;
The views folder is where I create the components that will be rendered.
The content of the file views/SignIn/index.tsx is:
import React, { useContext } from 'react';
import Link from 'next/link';
import { AuthContext } from '../../../contexts/auth';
const SignIn: React.FC = () => {
const { signed, signIn } = useContext(AuthContext);
async function handleSignIn() {
signIn();
}
return (
<div>
<Link href="Dashboard">Go back to Dashboard</Link>
<button onClick={handleSignIn}>Click me</button>
</div>
);
};
export default SignIn;
And the content of the file views/Dashboard/index.tsx is:
import React, { useContext } from 'react';
import Link from 'next/link';
import { AuthContext } from '../../../contexts/auth';
const Dashboard: React.FC = () => {
const { signed, signIn } = useContext(AuthContext);
async function handleSignIn() {
signIn();
}
return (
<div>
<Link href="SignIn">Go back to sign in page</Link>
<button onClick={handleSignIn}>Click me</button>
</div>
);
};
export default Dashboard;
I am able to access the context inside both /Dashboard and /SignIn, but when I press the link, the state comes back to the initial one. I figured out that the whole provider is rerenderized and therefore the new state becomes the initial state, but I wasn't able to go around this issue in a "best practices manner".
If I put the provider inside _app.tsx, I can maintain the state when transitioning between pages, but I end up providing this state to the / route as well, which I am trying to avoid.
I was able to go around this by doing the following, but it really does not seem to be the best solution for me.
I removed the Providers from Pages/SignIn/index.tsx and Pages/Dashboard/index.tsx and used the following snippet for the _app.tsx file:
import React from 'react';
import { AppProps } from 'next/app';
import { useRouter } from 'next/router';
import { AuthProvider } from '../contexts/auth';
const App: React.FC<AppProps> = ({ Component, pageProps }) => {
const router = useRouter();
const AuthProviderRoutes = ['/SignIn', '/Dashboard'];
return (
<>
{AuthProviderRoutes.includes(router.pathname) ? (
<AuthProvider>
<Component {...pageProps} />
</AuthProvider>
) : <Component {...pageProps} />}
</>
);
};
export default App;
Does anyone have a better solution?

Warning : update in state transition when accessing the app via a callback page and updating the Context from here

I have an app composed of two pages, a Home page and a Callback page. After authentication on a tiers service, such as a social network or other, the user is redirected to the Callback page of my app. The app uses the react-context api and I would like to store all the data found in the query string of the callback page. Shortly I want to open a page of my app, not the home page, and update the context of the app from this page.
This is what is achieved in this tutorial for Auth0. The app is composed of 5 very short files, I will reproduce them here.
The problem is that I get a warning when I access http://127.0.0.1:3000/callback in a browser. The warning is Warning: Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props and state.
In the Auth0 tutorial, this warning doesn't show up but the state update is made within a dedicated function, I don't know how to reproduce this work with my own components.
To reproduce the warning, use create-react-app and use the files at the end of the post.
The goal of this toy example is to set unused_data in the state of Context component, but without triggering the warning (unused_data is actually set).
EDIT : Well, I learned redux and everything works fine when I use redux, so I still don't know why I got an error, but everything is just so much simpler now :)
App.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import Context from "./Context";
import Callback from './Callback'
import Home from './Home'
class App extends Component {
render() {
return (
<div className="App">
<Context>
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/callback" component={Callback} />
</Switch>
</Router>
</Context>
</div>
);
}
}
export default App;
Context.js
import React, {Component} from "react";
import {Provider} from "./contextDefs";
class Context extends Component {
state = {
unused_data: ""
};
changeState = () => {
this.setValue()
}
setValue = () => {
this.setState({
unused_data: "has been set up",
})
}
render() {
const providerValue = {
...this.state,
changeState: this.changeState,
};
return (
<Provider value={providerValue}>
{this.props.children}
</Provider>
);
}
}
export default Context;
Callback.js
import React from "react";
import {Redirect} from "react-router-dom";
import {Consumer} from "./contextDefs";
const callback = props => (
<Consumer>
{({changeState}) => {
changeState();
return <Redirect to="/"/>;
}
}
</Consumer>
)
export default callback;
contextDefs.js
import { createContext } from "react";
const Context = createContext({
unused_data: "",
changeState: () => {},
});
export const Provider = Context.Provider;
export const Consumer = Context.Consumer;
Home.js
import React from "react";
const AuthTwitter = props => {
return (
<div>
Test
</div>
);
}
export default AuthTwitter;

Resources