Getting Global Access to URL ID Params - javascript

How do I get global access to the current URL ID params? I'm having trouble getting access to the current URL ID in a child component in React. The ID is needed in order to query a MongoDB database in my ChecklistTool component.
Normally, I'd pass the props and get access to params that way. However, I'm using Editor.js which is not letting me pass the props as required.
This is my Editor.js component. It has access to params information:
<EditorJs
instanceRef={(instance) => (instanceRef.current = instance)}
placeholder="Start typing what's in your head..."
tools={EDITOR_JS_TOOLS}
enableReInitialize={true}
data={data}
/>
This is my custom Checklist component which is rendered as a block inside of the Editor.js component. I'm unable to pass the URL params to this component:
<ChecklistTool
onDataChange={onDataChange}
readOnly={this.readOnly}
data={this.data}
isAdmin={true}
/>
This is the file where I'm using React Router:
<Router>
<Switch>
<Route exact path="/document/:id" component={DocumentView}/>
</Switch>
</Router>
Any idea how to get access to the current URL params ID in all required files? Thanks!

If you are using a version of React >= 16.8 then you can use hooks.
Instead of using props to pass the value, React-Router provides a hook called useParams which enables you to access the URL params from within any component (as long as it is wrapped inside the router), which makes it essentially global and accessible from any child without passing as props.
This is how you use it:
First you import useParams
import {useParams} from "react-router-dom";
Then call the hook
const params = useParams(); //:id param will be in params.id and so on...
If you have tried this approach and did not work for you, let me know more about the kinds of errors you received, if any, and I will look into them.
React-Router reference for more information about useParams hook.

Related

React router v6 access route params and pass as props

In react router v6, how can I pass route params to a component without the need to use useParams() in the component?
This is what I want to do:
<Route
path='/'
element={ProfileComponent username={'thedefault'}
/>
<Route
exact
path='/u/:username/'
render={(props) =>
<ProfileComponent username={props.match.params.username}/>
}
/>
I don't want to put useParams() in the component because this tightly couples it to the URL. For example, what if I wanted to render another ProfileComponent elsewhere, with a different username to that in the URL. It seems to violate best practice for unit testing unless I can do it like my example.
I don't want to put useParams() in the component because this
tightly couples it to the URL. For example, what if I wanted to render
another ProfileComponent elsewhere, with a different username to that
in the URL. It seems to violate best practice for unit testing unless
I can do it like my example.
Any route using a username route match param would still be accessible via the useParams hook, but I think I understand what you are after. If I understand your question correctly you are asking how to map a route match param to a component prop in a generic way.
For this you can simply use a wrapper component to "sip" the route match param and pass it along to your component on any specific prop.
const ProfileComponentWrapper = () => {
const { username } = useParams();
return <ProfileComponent username={username} />;
};
...
<Route
path='/u/:username/'
element={<ProfileComponentWrapper />}
/>
In the docs, it is clearly specified that it is not possible
Normally in React you'd pass this as a prop: , but you don't control that information because it comes from the
URL.
https://reactrouter.com/docs/en/v6/getting-started/tutorial#reading-url-params
So, you have to use useParams in the component

passing parameter to another component using Link in react

I know if I need to call another component then we need to pass like <A x={y}/> and we can access props.x inside A.
But here I need to call EditCertificate so I need to pass id to EditCertificate. but I am using Link here. I am not able to pass the id. when I am accesssing it, it is coming undefined.
<Link to={`/${props.certificate.id}/edit` } >Edit</Link>
and I am calling this page like below.
<Route path ="/:id/edit" component={EditCertificate} ></Route>
how can I access :id inside the EditCertificate .when I am accessing it is giving undefined. do I need to pass some other properties.
Since EditCertificate is rendered directly by the route:
<Route path ="/:id/edit" component={EditCertificate} />
the route props are passed to EditCertificate. You just need to access them from props.
const { id } = props.match.params;
if EditCertificate is a class component, then obviously access from this.props.
const { id } = this.props.match.params;
since you're using react-router you can simply import useParams hook and get the id in you EditCertificate component
import { useParams } from 'react-router-dom';
const { id } = useParams(); // add inside your component body

React extract param from url

I'm using react-router for routing . I've used NavLink and Route components like this:
<NavLink className={classes.navLink}
activeClassName={classes.activeNavLink}
to={`/brokers/${n.id}`}\>
....
<Route exact path="/brokers/:id" component={BrokerDetails} />
Now my question is - how do I use the id parameters passed in inside the BrokerDetails component ? I tried reading it via the props but it doesn't exist .
When using component=..., your component will be passed the route props.
In particular, you'll want to access the match object:
const BrokerDetails = ({match}) => <div>{JSON.stringify(match.params)}</div>;
should show you all the parameters; match.params.id would be the id parameter.
If you try to access props.Id won't work because it isn't in that location.
When you try to access params from an URL, which is passed using 'React-router-dom', then the way to access the props is match.params.id

React router redirects - pushState on the same or a different history object?

I'm trying to perform a redirect in a react component using react-router 1.0.2.
In my main app.js I create a hash history:
import createHashHistory from 'history/lib/createHashHistory';
const history = createHashHistory();
...
ReactDOM.render(
<RelayRouter history={history} routes={routes} />,
document.getElementById('root')
);
When I perform a database insert, I want to redirect to another page. I was using this which worked in an earlier version, but doesn't work any more:
import createHashHistory from 'history/lib/createHashHistory';
const history = createHashHistory();
...
// inside Component after a successful DB insert
history.pushState(null, `/#/albums/${newAlbum.id}`, null);
This is in another component (CreateAlbum).
Do I need to call pushState on the same history instance, and if so how can I get hold of it from within a component? If not, what am I doing wrong? The location bar does get updated, but the page stays the same.
Finally, I've manually prefixed /#/ to the URL. This is sloppy and will only work with a Hash History. I saw in the API there's a createHref method, but it expects a pathname which isn't explained. Given that the relevant route is defined as follows, how can I use that to generate a URL in the correct form as well?
<Route
component={App}
queries={ViewerQueries}
path="/">
<Route
path="/create" component={CreateAlbum} />
<Route
path="/albums/:albumId" component={AlbumDetails}
queries={ViewerQueries} />
</Route>
You have to call it on the same history instance.
If you're calling it in the Route components you defined in the component props of Route, such as your App, CreateAlbum or AlbumDetails, the history object is already available as this.props.history, you can console.log(this.props.history) to check it!
So you can simply call:
this.props.history.pushState(null, `/#/albums/${newAlbum.id}`)
to change route.
But if you want to call it in a nested component, you can...
Pass the history as a prop all the way down to your component:
<NestedComponent history={this.props.history} />
Use the history mixin if you're using react-router 1.0.2 (deprecated after 1.0.3)
checkout this SO for more detail
Use the router context's push method if you're using 1.0.4
If your component isn't really deep down, passing the history as prop might be easier!
To expand on Dax's answer,
The Router will pass the history component down to its nodes as a prop - hence this.props.history will be available on all your Route nodes. You shouldn't need to create a new history object for each component.
If you create a non-route component, you will need to pass the history into that component as Dax mentioned or pass some sort of relevant 'event' function from history as passing the entire object might not be totally necessary.
If you're already on 1.0.x - rackt has just released the rc for 2.0.0 as well which should be a fairly smooth upgrade. You can check out the upgrade guide here

Passing state to react-router 1.x

In react-router version 0.13.3 I have
Router.run(routes, createBrowserHistory, function(Handler, state) {
React.render(<Handler query={ state } path={ state.pathname }/>, document.getElementById('app'))
});
In react-router 1.0.0.rc3 I have
let history = createBrowserHistory();
React.render(<Router history={ history } >{ routes }</Router>, document.getElementById('app'));
With react-router 0.13.3, I was able to pass state to my Components with a callback. I was wondering how I can do the same with version react-router 1.x ?
React Router 1.0 now creates elements from your route components, passing them the following routing-related state as props by default:
history - a History object
location - a Location object
params - parameters extracted from the URL
route - the route object the rendered component belongs to
routeParams - parameters extracted from the URL for the route object the rendered component belongs to
routes - an array of all the route objects which were matched
You should be able to use these to get a hold of routing-related state, e.g. to get the pathname, you could use this.props.location.pathname inside a route component, or pass these onto children which need them.
The 1.0 upgrade guide has more details about this.
If you want to pass additional props to an individual route component when rendering it, you can use React.cloneElement() like so:
{React.cloneElement(this.props.children, {extra: 'extra prop'})
If you want to pass additional props when a route component element is being created, you can pass a custom createElement() function to <Router>.

Categories

Resources