Routes rerender if I use a map instead of static components - javascript

I have 2 routes in my app, /validations (which is a list) and /validations/:id (which is the same list focused on a particular item).
I initially wrote my routes statically and everything works (as expected):
<Route path="/validations/:id" component={Validations} />
<Route path="/validations" component={Validations} />
However, because I wanted to do some refactoring, I put the routes in an array and used a .map to render them. But with this, when I click on an item, the whole page rerender and I lose the scroll of the list (this is a bad UX for the user because he loses where he was in the list):
const routes = [
{
path: '/validations/:id',
component: Validations,
},
{
path: '/validations',
component: Validations,
},
]
// ...and in the JSX...
{routes.map(({ path, component }) => (
<Route
key={path}
path={path}
component={component}
/>
))}
Even stranger, if I remove the key prop of the Route I get back the initial behaviour (but I have a warning by React).
Did I write something wrong? Is it a react-router bug? A React one?
How can I keep the scroll of the list with my route mapping?
Here is a reproduction: https://codesandbox.io/s/vm71x2k46l

I knew something was fishy because I've done this many times and I didn't get your results. So I noticed that you render your sidebar inside your route.
This is why it's re-rendering and setting everything to the top. Because you actually navigate to another route.
What you must do is set your Sidebar links outside the actual routes but inside the Router Component.
Like in this sandbox: https://codesandbox.io/s/p2r4pl2o3q
If you notice I changed the routes a bit:
const routes = [
{
path: "/",
component: Index
},
{
exact: true,
path: "/validations",
component: Validations
},
{
path: "/validations/:id",
component: Validations
}
];
And then created the sidebar component and set it outside the routes
<BrowserRouter>
<div>
<Sidebar />
<div style={{ marginLeft: 150 }}>{routeComponents}</div>
</div>
</BrowserRouter>
Sidebar:
var items = Array.apply(null, Array(300)).map(function(x, i) {
return i;
});
export default () => (
<div
style={{
width: 150,
height: "100vh",
display: "flex",
overflow: "auto",
position: "absolute",
flexDirection: "column"
}}
>
{items.map((item, index) => (
<Link to={`/validations/${index}`} key={index}>
Item {index}
</Link>
))}
</div>
);
Now since first click the scroll doesn't reset.
PS: Please don't mind the styling decisions I was just trying to see something work.

First you need to map the routes outside the App Component. Then you can add them mapped routes in the App in example {routeComponents}
const routeComponents = routes.map(({ path, component }, key) => <Route exact path={path} component={component} key={key} />);
const App = () => (<BrowserRouter><div>{routeComponents}</div></BrowserRouter>);
Also in the validation.js you have warning in the {items.map ...} line. I added the key prop in the link and the error gone
{items.map((item, index) => (
<Link to={`/validations/${index}`} key={index}>
Item {index}
</Link>
))}
here is the sandbox url https://codesandbox.io/s/8n2rlpmw0l

Related

Params matching return empty React-router

I am facing an apparently simple problem but it is getting complicating , even with some well written existing topics.
I have a global route
export const createPricingPath = `${catalogRoutes.priceLists.path}/creation/:pricingId?/:step(products|parameters|customers-groups)`;
export const toCreatePricingPath = pathToRegexp.compile(createPricingPath);
As you can see I have a step parameters and an optional one called pricingId.
I have a parent route leading to my top level parent component
<ProtectedRoute
exact
AUTHORIZED_ROLES={[USER_ROLES.PRICING]}
path={createPricingPath}
render={props => <PricingCreationPanelContainer {...props} />}
/>
The parent top root component is the following one :
<SlidePaneProvider route={catalogRoutes.priceLists.path}>
{({ handleCancel }) => (
<Fragment>
<PricingCreationPanelHeader handleCancel={handleCancel} {...this.props} />
<SlidePaneBody>
<PricingContent handleCancel={handleCancel} {...this.props} />
</SlidePaneBody>
</Fragment>
)}
</SlidePaneProvider>
Inside this component we got somme "commun" elements like a PricingCreationHeader and SlidesComponents ( body ect...), and I have a PricingContent which is the children containing my sub-routes and children components.
It looks like this:
const PricingContent = ({ handleCancel, match: { params } }) => {
return (
<Switch>
<ProtectedRoute
exact
AUTHORIZED_ROLES={[USER_ROLES.PRICING]}
path={toCreatePricingPath({ step: 'parameters' })}
render={props => <PricingCreation {...props} />}
/>
<ProtectedRoute
exact
AUTHORIZED_ROLES={[USER_ROLES.PRICING]}
path={toCreatePricingPath({ step: 'products', pricingId: params.pricingId })}
render={props => <PricingProducts {...props} />}
/>
<ProtectedRoute
exact
AUTHORIZED_ROLES={[USER_ROLES.PRICING]}
path={toCreatePricingPath({ step: 'customers-groups', pricingId: params.pricingId })}
render={props => <PricingClients handleCancel={handleCancel} {...props} />}
/>
</Switch>
)
};
The problem is, I can't figure out why I am not able to get the match.params.pricingId and match.params.step props inside every child component (PricingClients and PricingProducts ). PricingCreation doesnt need pricingId by the way, only the other two.
I do have the impression that everything is clearly well written.
The child component should be able to get all the params since the root parent component does.
Maybe it is an architecture problem so every suggestion is welcome !
I went through many existing topics and was not able to find a proper solution.
Thank you very much for helping me !
As far as I can see here, path for child routes is specified incorrectly. I dont think that making path dynamically is a good idea. It is enough to specify shortened version of parent path and match.pricingId will be available in child components:
const PricingContent = ({ handleCancel }) => {
return (
<Switch>
<ProtectedRoute
path={ `${base_path }/creation/:pricingId?/parameters`}
// other props stay the same
/>
<ProtectedRoute
path={ `${base_path}/creation/:pricingId?/products`}
// other props stay the same
/>
<ProtectedRoute
path={ `${base_path}/creation/:pricingId?/customer-groups`}
// other props stay the same
/>
</Switch>
)
};
According to code above base_path is catalogRoutes.priceLists.path.
I think there is no need in :step route param as we have a separate component for every route.
In case we still need it, it's possible to do smt like this: .../:step(parameters);
Optional :pricingId? can cause mounting of incorrect component, for example:
base_url/products/parameters
In this case router is considering "products" as :pricingId route param which is not good=)
I often try to avoid optional params in the middle of a path
My apologies if I missed or misunderstood something
Thank you

React Router. Why, when changing the route, the component is rendered 2 times, which causes 2 requests to the server?

I am using ReactTransitionGroup with ReactRouter.
The goal is to reroute smoothly from one component to another. The problem - is that the component is rendered twice.
An example of a component (view) that renders twice
I am using the console to check. You might say that this is not critical. But, the problem is that because of this problem, 2 requests go to the server (one extra). Therefore, it is desirable for me to get rid of this bug.
This is the component itself - the switch
When switching a route, the console issues logs twice
I need to figure out why the side effect is being called twice. If there is not enough information, then write comments. I will try to answer as quickly as possible.
Here's an example from the documentation. I have achieved this effect, but the problem has already been described.
UPD: I remember very well that once it worked like a clockwork. But, probably, I myself did not notice that I changed something, which led to this problem.
UPD: If you need a code, then please, the required elements:
const TabList = ({ tabs }) => {
return (
<nav className="p-my-company__tabs">
{tabs.map(({ to, label, id }) => (
<NavLink to={to} key={id}>
<div>{label}</div>
</NavLink>
))}
</nav>
);
};
const TabViews = ({ tabViews }) => {
const location = useLocation();
return (
<div className="p-my-company__views">
<TransitionGroup>
<SwitchTransition mode="out-in">
<CSSTransition
key={location.pathname}
classNames={{
enter: 'move-enter',
enterActive: 'move-enter-active',
exit: 'move-exit',
}}
timeout={100}>
<Switch>
{tabViews.map(({ path, Component, id }) => (
<Route path={path} render={() => <Component />} key={id} />
))}
</Switch>
</CSSTransition>
</SwitchTransition>
</TransitionGroup>
</div>
);
};
<div className="p-my-company__panel">
<TabList
tabs={[
{ to: ROUTES.COMMON_INFO, label: 'Общая информация', id: 1 },
{ to: ROUTES.SHOWCASE, label: 'Моя витрина', id: 2 },
]}
/>
<TabViews
tabViews={[
{ path: ROUTES.COMMON_INFO, Component: CommonView, id: 1 },
{ path: ROUTES.SHOWCASE, Component: ShowCaseView, id: 2 },
]}
/>
</div>
const ShowCase = () => {
useEffect(() => {
console.log(2);
}, []);
return <div>ShowCase</div>;
};
Looks like the Switch component from React Router and React Transition Group don't work well together. The docs recommend avoiding the usage of the Switch component and passing a function to the Route's children prop. Since the function will be called regardless of whether there is a match or not, you can conditionally render Component if there's one.
<>
{tabViews.map(({ path, Component }) => (
<Route exact path={path} key={path}>
{({ match }) => (
<TransitionGroup>
<SwitchTransition mode="out-in">
<CSSTransition
in={match != null}
classNames={{
enter: 'move-enter',
enterActive: 'move-enter-active',
exit: 'move-exit',
}}
timeout={100}
unmountOnExit
key={location.pathname}
>
<div className="page">{match && <Component />}</div>
</CSSTransition>
</SwitchTransition>
</TransitionGroup>
)}
</Route>
))}
</>

How do i display Breadcrumb name conditionally on response from child.in React with hooks?

I am currently working on a project with React Hooks.
Parent component is a Navigator
Child component is a breadcrumb display in this navigator.
Child component fetches and displays a view with the data.
How can i use the response data in the 2. child component to set name in the 1. Child component?
My Code (omitted large portions of unnecessary code for this example):
Navigator
const { Header, Content } = Layout;
const Navigation = (props: any) => (
<>
<Layout>
<Layout>
<Header>
<Breadcrumbs
style={{ flexGrow: 2, paddingLeft: 20 }}
name='Name of User'
/>
</Header>
<Content style={{ margin: '24px 16px 0', overflow: 'hidden' }}>
<div className="content">
<Switch>
<Route exact path="/" component={MyPatients} />
<Route exact path="/Skjema" component={MySchemas} />
<Route
exact
path="/Pasient"
component={() =>
<PatientInfo
patientID={props.history.location.state}
/>
}
/>
export default withRouter(Navigation);
BreadCrumbs
import React from 'react';
import Breadcrumb from 'antd/lib/breadcrumb';
import { HomeOutlined, UserOutlined } from '#ant-design/icons';
const Breadcrumbs = (props: any) => {
return (
<>
<div className="Breadcrumbcontainer" style={props.style}>
<Breadcrumb>
<Breadcrumb.Item href="/">
<HomeOutlined />
<span style={{ color: 'black' }}>Hjem</span>
</Breadcrumb.Item>
<Breadcrumb.Item href="Pasient">
<UserOutlined />
<span style={{ color: 'black' }}>
{props.name}
</span>
</Breadcrumb.Item>
<Breadcrumb.Item>
<span>Skjema 1 - 17.04.20</span>
</Breadcrumb.Item>
</Breadcrumb>
</div>
</>
);
};
export default Breadcrumbs;
The third file contains a fetch to an api and works fine, the data in question is currently stored as response.name How can i lift this info up to Navigator?
If I understood your question correctly, there's a parent component that has two child components and you want to trigger a change from one child component in another.
You can maintain the state in the parent component, pass state in child1 and setState function in child2.
// Parent Component
const [name, setName] = useState('');
<>
<child1 name={name}/>
<child2 setName={setName}/>
</>
Try this one. I added code sandbox and you can check it out is this what you need. So from parent pass hook as props to a child, and then after the request is made inside of the child component call function from props that will fill the data inside parent component.
Update state in parent from child
I found a solution thanks to the suggestions in this thread. I made my mistake in the passing of the files to the function.
const PatientInfo = ({ patientID, setName }: any) => {
console.log(useFetch<IPatient>( // name of endpoint.... ));
const { response } = useFetch<IPatient>(
'// name of endpoint.... + patientID,
patientID,
);
This ended up fixing my problem. The problem was i initially called the two seperately, like this:
const PatientInfo = ({ patientID }: any, { setName } : any) => {
console.log(useFetch<IPatient>( // name of endpoint.... ));
const { response } = useFetch<IPatient>(
'// name of endpoint.... + patientID,
patientID,
);
So this change worked, although I am not entirely sure as to why.

How to render same react component for different routes with different value props?

I want to ask whether it is possible to render same react component for different routes but with different value props:
I have something like this:
<Switch>
<Route
exact
path="/something1"
render={props => (
<SomeComponent
{...props}
buttonStyle="#65BDE0"
/>
)}
/>
<Route
exact
path="/something2"
render={props => (
<SomeComponent
{...props}
buttonStyle="#FFFFFF"
/>
)}
/>
<Route
exact
path="/something3"
render={props => (
<SomeComponent
{...props}
buttonStyle="#000000"
/>
)}
/>
</Switch>
As you can see I have three different routes with the same component but the buttonStyle is different for each route. Is there a way how to simplify this or some better apporach how to handle this ? For example with one Route component ? Thank you.
I doubt that you can do this with one <Route /> component since a Route can have only one path.
However, you could make it a bit cleaner by creating an array of routes.
It could look something like this:
const routesWithProps = [
{
path: '/something1',
props: {
buttonStyle: '#65BDE0'
}
},
{
path: '/something2',
props: {
buttonStyle: '#FFFFFF'
}
},
{
path: '/something3',
props: {
buttonStyle: '#000000'
}
},
];
<Switch>
{
routesWithProps.map(route => (
<Route
exact
path={route.path}
key={route.path}
render={props => <SomeComponent {...props} {...route.props} />}
/>
))
}
</Switch>
That would depend on where you want to keep the logic. If you want to make the route simple you could have a single route component with something/:btn, and then in the <SomeComponent /> have the button rendering logic. You could have the /:btn be the colored hash you want the button to be in, or some arbitary id number only your app could decipher.
An example would be
export const SomeComponent = ({history}) => {
const param = history.params.btn;
if(param === 1) {
return <div>Here i can add specific things </div>
}
// and so on
}
This is not because it is the best solution, but maybe this idea will kickstart how to want to do it. Good luck

How to render React Route component in an entirely new, blank page

I'm trying to render a print page using React Router. So I have two components:
export default class PurchaseOrder extends React.Component{
....
render(){
const {orderDate, client} = this.state.order;
//omitted for brevity
return(
<BrowserRoute>
<Button
component={Link}
to="/order/print"
target="_blank"
>
Print
</Button>
<Route
path="/order/print"
render={props => (
<OrderPrint
{...props}
orderDate={orderDate}
client={client}
/>
)}
/>
</BrowserRoute>
}
}
And the OrderPrint:
export default function OrderPrint(props) {
return (
<div>props.orderDate</div>
<div>props.client.name</div>
);
}
As you can see, I'm trying to present the printable version of the purchase order with a click of a button. The OrderPrint component gets rendered, but it's rendered right below the button. I could put the Route inside my root component, which is App, that way making sure that I get only the contents of the OrderPrint component rendered like this:
class App extends Component {
render() {
return (
<div className="App">
<Router>
<Route exact path="/" component={PurchaseOrder} />
<Route exact path="/order/print" component={OrderPrint} />
</Router>
</div>
);
}
}
But in that case, I won't be able to pass the necessary props to it. So in this particular case, how to replace entire page content with the contents of OrderPrint component and still be able to pass the necessary input to it?
Update
As #Akalanka Weerasooriya mentioned in comments, I could have the entire state kept in the App component. But one thing stopped me from doing this: This means I'll practically always have to use the render prop of the Route component, instead of the component prop. Ok, that's not a problem, but if it's the way to go, then why does React Router documentation almost always use the
<Route path="/about" component={About} />
pattern as the standard way of using it? So to recap it, if I go the Single Source of Truth way and store all my state in one place, then doesn't it mean that I will always use
<Route path="/about" render={props=>(<div>props.someProp</div>)} />
I don't say there's a problem with it, it's just mentioning it in the documentation only after component={SomeComponent} pattern confuses me.
Not sure why you need a different route for a print page, but anyway if you want it on a new empty page, you can take advantage of the ReactDOM.createPortal feature.
You can create a new page and or even a new window using window.open while keeping the flow of react data in sync.
Here is a running example of a portal on a new window with live state updates from the component that triggered this window using a portal:
running example, i'm sharing an external snippet and not using stack-snippets here because window.open returns null in the contexts of stack-snippets
Source code:
class WindowPortal extends React.PureComponent {
containerEl = document.createElement("div");
externalWindow = null;
componentDidMount() {
const { width = 450, height = 250, left = 150, top = 150 } = this.props;
const windowFetures = `width=${width},height=${height},left=${left},top=${top}`;
this.externalWindow = window.open("", "", windowFetures);
this.externalWindow.document.body.appendChild(this.containerEl);
}
componentWillUnmount() {
this.externalWindow.close();
}
render() {
return ReactDOM.createPortal(this.props.children, this.containerEl);
}
}
class App extends React.PureComponent {
state = {
counter: 0,
showWindowPortal: false
};
componentDidMount() {
window.setInterval(() => {
this.setState(state => ({
counter: state.counter + 1
}));
}, 1000);
}
toggleWindowPortal = () => {
this.setState(state => ({
...state,
showWindowPortal: !state.showWindowPortal
}));
};
closeWindowPortal = () => {
this.setState({ showWindowPortal: false });
};
render() {
return (
<div>
<h1>Counter: {this.state.counter}</h1>
<button onClick={this.toggleWindowPortal}>
{this.state.showWindowPortal ? "Close the" : "Open a"} Portal
</button>
{this.state.showWindowPortal && (
<WindowPortal closeWindowPortal={this.closeWindowPortal}>
<h2>We are in a portal on a new window</h2>
<h3>{`This is the current state: ${this.state.counter}`}</h3>
<p>different window but sharing the state!!</p>
<button onClick={() => this.closeWindowPortal()}>Close me!</button>
</WindowPortal>
)}
</div>
);
}
}
here you have a PrivateRoute which is a custom route which holds a header and header is rendered in PrivateRoute routes only so when you try to navigate to new route like path="/order/print" then you won't get header which has button in it.
function Header(props) {
return (
<div>
<Button
component={Link}
to="/order/print"
target="_blank">
Print</Button>
{props.children}
</div>
)
}
const PrivateRoute = ({ component: Component, layout: Layout, ...rest }) => {
return <Route {...rest} render={props => {
return <Layout>
<Component {...props} />
</Layout>
}} />
}
export default class PurchaseOrder extends React.Component{
render(){
const {orderDate, client} = this.state.order;
//omitted for brevity
return(
<BrowserRouter>
<div>
<PrivateRoute exact path="/" layout={Header} component={Landing} />
<Route
path="/order/print"
render={props => (
<OrderPrint
{...props}
orderDate={orderDate}
client={client}
/>
)}
/>
</div>
</BrowserRouter>
}
}

Categories