Animate route change in react-router-dom switch component - animation

I'm having a really hard time animating the transition from one page to another with react-router-dom. The exmaple is fine but I can't get it to work within a Switch component provided by react-router-dom.
I've tried doing this around the Switch component or inside it but it doesn't do anything (also no warnings or errors in the console).
Example
class Layout extends PureComponent {
render() {
const { account } = this.props;
return (
<div className="MAIN">
<Header image={account.resources.logo} backgroundColor={account.theme} />
<ProgressionBar />
<div className="MAIN__content">
<CSSTransition classNames="fade" timeout={{ enter: 1500, exit: 500 }}>
<Switch key={this.props.location.key} location={this.props.location}>
<Route exact path={`${basePath}start`} component={Start} />
<Route exact path={`${basePath}questions`} component={Questions} />
<Route exact path={`${basePath}comments`} component={Comments} />
<Route exact path={`${basePath}capture`} component={Capture} />
<Route exact path={`${basePath}disclaimer`} component={Disclaimer} />
<Route exact path={`${basePath}finish`} component={null} />
</Switch>
</CSSTransition>
<Footer />
</div>
</div>
);
}
}
CSS
.fade-enter {
opacity: 0.01;
}
.fade-enter.fade-enter-active {
opacity: 1;
transition: opacity 500ms ease-in;
}
.fade-exit {
opacity: 1;
}
.fade-exit.fade-exit-active {
opacity: 0.01;
transition: opacity 300ms ease-in;
}

Try this:
<CSSTransition classNames="test" transitionLeaveTimeout={300}>
<Switch key={this.props.location.pathname} location={this.props.location}>
...
</Switch>
</CSSTransition>

try to rename classNames to transitionName.
and the css .fade-exit also should be .fade-leave if you are using these libraries: https://facebook.github.io/react/docs/animation.html or not?

Thanks #Melounek for the help, the issue resided in that I needed to wrap the CSSTransition in a TransitionGroup as shown in the migration docs of react-transition-group.
Example
class Layout extends PureComponent {
render() {
const { account, location } = this.props;
return (
<div className="MAIN">
<Header image={account.resources.logo} backgroundColor={account.theme} />
<ProgressionBar />
<div className="MAIN__content">
<TransitionGroup>
<CSSTransition
key={location.key}
classNames="fade"
timeout={{ enter: 1000, exit: 1000 }}
transitionEnterTimeout={1000}
transitionLeaveTimeout={1000}
>
<Switch key={location.key} location={location}>
<Route exact path={`${basePath}start`} component={Start} />
<Route exact path={`${basePath}questions`} component={Questions} />
<Route exact path={`${basePath}comments`} component={Comments} />
<Route exact path={`${basePath}capture`} component={Capture} />
<Route exact path={`${basePath}disclaimer`} component={Disclaimer} />
<Route exact path={`${basePath}finish`} component={null} />
</Switch>
</CSSTransition>
</TransitionGroup>
<Footer />
</div>
</div>
);
}
}

Try this:
<CSSTransition key={this.props.location.pathname.split('/')[1]} timeout={500} classNames="fadeTranslate" mountOnEnter={true} unmountOnExit={true}>
<div className="WRAPPER">
<Switch location={this.props.location}>
<Route path="/" exact component={Home} />
<Route path="/blog" component={Blog} />
<Route path="/albumn" component={Albumn} />
</Switch>
</div>
</CSSTransition>
Reference: https://github.com/ReactTraining/react-router/issues/5279#issuecomment-315652492

Related

React Router Dom v6: Scroll To Top on Route Change [duplicate]

This question already has answers here:
react-router scroll to top on every transition
(35 answers)
Closed 11 months ago.
I made a website on Reactjs and I have linked various pages using 'react-router-dom v6'. Whenever I transition from one page to another, the second page always loads on the current location instead of the top. I have tried many tutorials and solutions but they all are valid till v5 of react-router-dom.
I solved the following issue by creating a wrapper function and wrapping it around all the routes.
Follow the following steps:
1: You need to import the following:
import {Routes, Route, BrowserRouter as Router, useLocation} from 'react-router-dom';
import {useLayoutEffect} from 'react';
2: Write a wrapper function just above the "App" function:
const Wrapper = ({children}) => {
const location = useLocation();
useLayoutEffect(() => {
document.documentElement.scrollTo(0, 0);
}, [location.pathname]);
return children
}
3: Now wrap your routes within the wrapper function:
<BrowserRouter>
<Wrapper>
<Navbar />
<Routes>
<Route exact path="/" element={<Home/>} />
<Route path="/Products" element={<Products/>} />
<Route path="/Login" element={<Login/>} />
<Route path="/Aggressive" element={<Aggressive/>} />
<Route path="/Attendance" element={<Attendance/>} />
<Route path="/Choking" element={<Choking/>} />
<Route path="/EmptyCounter" element={<EmptyCounter/>} />
<Route path="/FaceMask" element={<FaceMask/>} />
<Route path="/Fainting" element={<Fainting/>} />
<Route path="/Smoking" element={<Smoking/>} />
<Route path="/SocialDistancing" element={<SocialDistancing/>} />
<Route path="/Weapon" element={<Weapon/>} />
</Routes>
<Footer />
</Wrapper>
</BrowserRouter>
This should solve the issue.

React Router - need help routing

My problem:
If I open http://example.test/profile/create, the New Profile component does not get loaded. Same with the view and edit routes.
In fact, if I click on a Link to profile/create it displays an empty space beneath the Header component. If I try to go to the above address directly via url or by refreshing the page, it shows 404 page not found error.
The Login and Register components work absolutely fine as does the ProfileList (Clicking on a Link or direct navigation both work fine).
<BrowserRouter>
<div>
<Header />
<Switch>
<Route exact path='/' component={ProfilesList} />
<Route exact path='/login' component={LoginComponent} />
<Route exact path='/register' component={RegisterComponent} />
<Route exact path='/profile'>
<Route exact path='/create' component={NewProfile} />
<Route exact path='/view'>
<Route path='/:id' component={ProfileDetail} />
</Route>
<Route exact path='/edit'>
<Route path='/:id' component={EditProfile} />
</Route>
</Route>
</Switch>
</div>
</BrowserRouter>
PS - The below code worked for http://example.test/create AND for http://example.test/1:
<BrowserRouter>
<div>
<Header />
<Switch>
<Route exact path='/' component={ProfilesList} />
<Route path='/login' component={LoginComponent} />
<Route path='/register' component={RegisterComponent} />
<Route path='/create' component={NewProfile} />
<Route path='/:id' component={ProfileDetail} />
</Switch>
</div>
</BrowserRouter>
I'm using Laravel 7 to run my server. The routes/web.php file looks like this -
<?php
Route::view('/{path?}', 'app');
Here's hoping this helps someone.
The key lay in adding a match.path variable. Firstly my App.js file (the entry
place for my app) -
class App extends Component {
render () {
return (
<BrowserRouter>
<div>
<Header />
<Switch>
<Route exact path='/' component={ProfilesList} />
<Route path='/login' component={LoginComponent} />
<Route path='/register' component={RegisterComponent} />
<Route path='/profile' component={Profiles} />
</Switch>
</div>
</BrowserRouter>
)
}
}
Next the Profiles.js file which is handling the routing for profiles link.
const Profiles = ({match}) => {
return (
<BrowserRouter>
<Switch>
<Route path={`${match.path}/create`} component={NewProfile}/>
<Route path={`${match.path}/view`} render={({match}) => (
<Route path={`${match.path}/:id`} component={ProfileDetail}/>
)} />
<Route path={`${match.path}/edit`} render={({match}) => (
<Route path={`${match.path}/:id`} component={EditProfile}/>
)} />
</Switch>
</BrowserRouter>
)
}
export default Profiles
I am still running into the problem of browser throwing 404s when trying to visit the url manually. I know that the answer lies in this answer by Stijn de Witt - I most likely want to do the catch-all solution that he has proposed, but I don't know how to do that. Can someone help?

REST URL customization

I had to change in the restclient after like this:
var parentresource = '';
switch (resource) {
case 'details':
parentresource = `products/${params.id}/${resource}`;
break;
default:
parentresource = `${resource}`;
}**
url = `${apiUrl}/${**parentresource**}?${fetchUtils.queryParameters(query)}`;
break;
I have a EditView where in one tab I have got:
<ReferenceManyField addLabel={false} reference="details" target="id">
<Datagrid>
<NumberField source="quantity" label="Quantity" />
<TextField source="measurementUnit" label="Measurement" />
<NumberField source="purchasePrice" label="Purchase Price" />
<NumberField source="salesPrice" label="Sales Price" />
<NumberField source="discountPercent" label="Discount Percent" />
<NumberField source="salesTaxPercent" label="Sales Tax Percent" />
<TextField source="supplier" label="Supplier" />
<DateField source="createdDate" label="Created Date" />
<TextField source="status" label="Status" />
<EditButton basePath="/products" />
</Datagrid>
</ReferenceManyField>
When I click EditButton the url in the browser needs to needs to be http://localhost:5000/#/details/1?_k=5f7qc1 but the url to grab data from should be http://localhost:5000/api/products/2/details/1
Any easier way to do this?
That's the job of a custom restclient

Phonegap images not showing

I'm having trouble getting my images to work in my phonegap build.
I've read that the absolute paths might not work so i've tried both absolute and relative paths, still no luck.
I'm including the images like this:
<Col key={1} xs={3}>
<Image src='/tire_selected.png' responsive />
</Col>
or relative
<Col key={1} xs={3}>
<Image src='tire_selected.png' responsive />
</Col>
equals
<img class="img-responsive" src="tire_deselected.png" data-reactid=".0.0.1.0.0.0.0.1.1.0.0.$4.0">
Col & Image is bootstrap helper components using bootstrap-react. And this all works fine in the web view, but not when built with phonegap. It should though, the source is already compiled and without errors in both cases.
Following is my config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.app.exampleapp" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>App</name>
<description>
App
</description>
<author email="support#example.com" href="http://www.example.com">
Author
</author>
<content src="index.html" />
<preference name="permissions" value="none" />
<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="webviewbounce" value="true" />
<preference name="prerendered-icon" value="true" />
<preference name="stay-in-webview" value="false" />
<preference name="ios-statusbarstyle" value="black-opaque" />
<preference name="detect-data-types" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="show-splash-screen-spinner" value="true" />
<preference name="auto-hide-splash-screen" value="true" />
<preference name="disable-cursor" value="false" />
<preference name="android-minSdkVersion" value="14" />
<preference name="android-installLocation" value="auto" />
<gap:plugin name="org.apache.cordova.geolocation" />
<icon src="icon.png" />
<access origin="*" />
<plugin name="cordova-plugin-whitelist" version="1" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
</widget>
Git repository:
app.js
vendor.js
config.xml
favicon.ico
humans.txt
index.html
robots.txt
tire_deselected.png
tire_selected.png
Icon.png works fine though. I have no idea whats causing the other images to not work. Any help would be appreciated!
Edit
I've tried setting content-security-policy, if that was the issue that i weren't able to set img-src and display images via javascript.
<meta http-equiv="Content-Security-Policy" content="
default-src http://10.3.10.104/ 'self' * 'unsafe-inline';
style-src http://10.3.10.104/ 'self' * 'unsafe-inline';
img-src http://10.3.10.104/ 'self' * 'unsafe-inline';
script-src http://10.3.10.104/ 'self' * 'unsafe-inline';">
But still no luck
file:///tire_deselected.png net::ERR_FILE_NOT_FOUND
There file is there, because when inserting an img-element into index.html it's displayed.
I even tried accessing it by the path that's displayed in the source folder running developer tools.
file:///data/user/0/com.oas.exampleapp/files/downloads/app_dir/tire_deselected.png
Doesn't work either, i'm starting to think that phonegap is broken, atleast works very poorly in combination with react.
After compilation the build.phonegap.com put your source files into "www" directory.
You can access your local image file using the following path "/android_asset/www/"
<image src='/android_asset/www/tire_selected.png' responsive />
If your image is placed in a subdirectory inside the root direcctory then you can use the following:
<image src='/android_asset/www/sub-direcctory/tire_selected.png' responsive />
Note: replace the "sub-direcctory" with your own if there is any in which the local image file is contained.
I added an img tag to index.html and set the src attribute to "images/logo.png" and it loads without issue.
...
<body>
<div id="root"></div>
<img id="footer-logo" src="images/logo.png" style="background-color: black; width: 200px;" />
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script src="js/vendor.bundle.js"></script>
<script src="js/app.bundle.js?v=2"></script>
</body>
</html>
I have a react component with an img tag and the same src value "images/logo.png"
...
<div style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0px; padding-top: 0px; letter-spacing: 0px; font-size: 24px; font-weight: 400; color: rgb(48, 48, 48); height: 64px; line-height: 64px; flex: 1 1 0px; text-align: center;">
<img id="header-logo" src="images/logo.png" style="width: 105px; margin-top: 16px;">
</div>
...
The img in the react component doesn't load; 404. Yet this equates to true
document.getElementById('footer-logo').src === document.getElementById('header-logo').src
How is it that one of the images loads and the other doesn't? Does it have something to do with the react component being loaded into the DOM dynamically or react's virtual DOM?
The src attributes equate to file:///images/logo.png. IF I set the src attribute on the #header-logo like this, it loads:
document.getElementById('header-logo').src = cordova.file.applicationDirectory + "www/images/logo.png"
Hope this provides more info to this very bizarre behaviour.
Hope this helps. So I also had the problem.
What I did was, was to create a another folder /images/ (duplicate) and still use my images I imported via react via my /static/components/images folder. You can take it a bit further by adding conditionals for staging or live.
So the answer is here.:
import Logo from '../images/logo.png';
<img src={`./${Logo}`} alt="Logo" />
Full example.:
import React, { Component } from 'react';
import Logo from '../images/logo.png';
class Header extends Component {
render() {
return(
<div className="logo mt-3">
<img src={`./${Logo}`} alt="Logo" />
</div>
);
}
}
export default Header;
Got the idea from this post.: Images not showing in PhoneGap Build application

Zk how to reach included .zul page component by id?

I can't reach component by id in the included .zul page. I have one main.zul with a controller and I need to get a component in included zul page through the java controller class, but it returns null.
I know the included method creates new id space but is there any way to get this component?
UPDATE
Here is my code:
the main zul page
<?page title="DealerVizard.zul"?>
<?page id="main" ?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" arg0="./Dealer" ?>
<zk>
<style src="/resources/css/default.css" />
<window id="Dealer" class="index"
apply="com.i2i.prm.controller.IndexController">
<div class="content" width="100%">
<tabbox id="tb" forward="onSelect=onSelect">
<tabs id="tabs">
<tab id="info" label="INFO" />
<tab id="create" label="CREATE" />
<tab id="edit" label="EDIT" />
<tab id="test" label="TEST PANEL(LIST BOX)" />
</tabs>
<tabpanels>
<tabpanel id="DealerInfo">
<include id="DealerInfoContent"
src="View/Dealer/DealerInfo.zul" />
</tabpanel>
<tabpanel id="DealerCreate">
<include id="DealerCreateContent"
src="View/Dealer/DealerCreate.zul" />
</tabpanel>
<tabpanel id="DealerEdit">
<include id="DealerEditContent"
src="View/Dealer/DealerEdit.zul" />
</tabpanel>
<tabpanel id="PagingListBox">
<include id="PagingListBoxContent" // Included here
src="View/TEST/PagingListBox.zul" />
</tabpanel>
</tabpanels>
</tabbox>
</div>
</window>
</zk>
PagingListBox.zul (Included page)
<?page id="list" ?>
<zk>
<grid width="100%">
<columns>
<column label="" />
</columns>
<rows>
<row>
<listbox id="listModel" width="100%" height="100%"
visible="true" span="true" pagingPosition="top" rows="20"
selectedItem="#{DealerController.selected}"
model="#{DealerController.userList}"
forward="onSelect=//main/Dealer.onSelect">
<auxhead>
<auxheader colspan="1">
<textbox
value="#{DealerController.searchUser.name}" maxlength="9"
id="searchCO_ID" forward="onChanging=//main/Dealer.onSearch"
width="100%">
</textbox>
</auxheader>
<auxheader colspan="1">
<textbox
value="#{DealerController.searchUser.surname}" maxlength="21"
id="searchMSISDN" forward="onChanging=//main/Dealer.onSearch"
width="100%">
</textbox>
</auxheader>
<auxheader colspan="1">
</auxheader>
</auxhead>
<listhead>
<listheader label="Name"
sort="auto(UPPER(name))" />
<listheader label="Surname"
sort="auto(UPPER(surname))" />
<listheader label="Delete ?" />
</listhead>
<listitem self="#{each=USERLIST}">
<listcell>
<label value="#{USERLIST.user.name}" />
<textbox
value="#{DealerController.tmpUser.name}" visible="false" />
</listcell>
<listcell>
<label value="#{USERLIST.user.surname}" />
<textbox
value="#{DealerController.tmpUser.surname}" visible="false" />
</listcell>
<listcell>
<button label="Update"
forward="onClick=//main/Dealer.onUpdate" visible="false" />
<button image="icons/edit-delete.png"
label="Delete" forward="onClick=//main/Dealer.onDelete"
width="100%" disabled="true" />
<button label="Save"
forward="onClick=//main/Dealer.onSave" visible="false" />
<button label="Cancel"
forward="onClick=//main/Dealer.onCancel" visible="false" />
</listcell>
</listitem>
</listbox>
<paging id="pagingData" pageSize="20" />
</row>
</rows>
</grid>
</zk>
IndexCOntroller.java
public class IndexController extends GenericForwardComposer {
private List<User> userList = new ArrayList<User>() ;
AnnotateDataBinder binder;
Tabbox tb;
Window Dealer;
private int pageCount=0;
#Override
public void doAfterCompose(Component comp) throws Exception {
// TODO Auto-generated method stub
super.doAfterCompose(comp);
comp.setVariable(comp.getId() + "Controller", this, true);
binder = (AnnotateDataBinder) Dealer.getVariable("binder", true);
System.out.println(Path.getComponent("//list/listModel"));
}
public IndexController() {
// TODO Auto-generated constructor stub
}
}
Normally I wouldn't recommend using Path.getComponent() way to access other components as your application code becomes tightly coupled with your component structure in your view page.
In your case you simplest way is to use AbstractComponent#getFellow(String compId) method so for eg.
Include inc = (Include) Dealer.getFellow("PagingListBoxContent");
Listbox listModel = (Listbox) inc.getFellow("listModel");
System.out.println(listModel);
So in future even if you insert any other component in your ZUML page before your listbox your code will still work.
UPDATE: BTW there was an interesting blogpost on this very topic on ZK blog recently
if your include have id, you can use dollar sign to get the inner components
<zk>
<include id="inc" src="test.zul />
</zk>
test.zul
<zk>
<label id="lab1" value="test1" />
</zk>
you can use "inc$lab1" get the label in test.zul
You can access any component in any other id space using zscript or java. if it is on the same page, but different window then (component B in window A):
Path.getComponent("/A/B");
if it is on a different page then (component B in window A on page P):
Path.getComponent("//P/A/B");
You can find documentation here: http://books.zkoss.org/wiki/ZK%20Developer%27s%20Reference/UI%20Composing/ID%20Space
You can add in your IndexController.java:
...
private Include DealerInfoContent;
...
this way you can access the included component within the parent composer.
(I would suggest to use camelCase ids for it, though).

Resources