Answer the question
In order to leave comments, you need to log in
How to implement animation in the actual page when navigating?
I want to implement the functionality as in dtf , where between the transitions over the pages, the loading animation under the Header in the actual page is triggered.
But I don't quite understand why it has such an effect. Obviously, there are not standard NavLinks here, because when you use it, you immediately jump to another page.
As I understand it, this effect is achieved using useHistory when, with a get request, animation is first turned on, and then when the data has arrived successfully, does it throw you?
Unfortunately, I do not have the opportunity to test this theory now, but the question still does not let me go.
Answer the question
In order to leave comments, you need to log in
Obviously, there are not standard NavLinks here, because when you use it, you immediately jump to another page.
<React.Suspense fallback={<>Loading...</>}>
React.lazy()
import('./ImportedPage')
// index.jsx
// "const Page = ..." - это концепция, в реальности в роутере
// можно получить название страницы (или id), которые и будут названием файла.
import React from 'react';
function App() {
const Page = React.lazy(() => import('./ImportedPage'))
return (
<div>
<React.Suspense fallback={<>Loading...</>}>
<Page />
</React.Suspense>
</div>
);
}
export default App;
// ImportedPage.jsx, то что импортирует главный компонент.
import React from 'react';
function Page() {
return (
<div>
<h1>Some Page, preloading required</h1>
</div>
);
}
export default Page;
import React from 'react';
import { Switch, Route, Link, useHistory } from 'react-router-dom';
import BarLoader from 'react-bar-loader';
export default () => {
const [data, setData] = React.useState();
const [isLoading, toggleLoading] = React.useState(false);
const history = useHistory();
const getData = () => (
new Promise((res, rej) => {
// Здесь можем выполнять любую нужную операцию - например, запрос к серверу
setTimeout(() => res('Some data, may be from server...'), 2000);
}).then(data => setData(data))
);
const handleLinkClick = (e) => {
toggleLoading(true)
e.preventDefault();
const pathname = e.target.pathname;
getData()
.then(data => {
toggleLoading(false)
history.push(pathname)
})
};
// Просто обертка над Link, чтобы не писать всегда onClick={handleLinkClick}
const WaitingLink = (props) => <Link onClick={handleLinkClick} {...props}>{props.children}</Link>
return (
<div>
<header>
{
isLoading && <BarLoader color='#1D8BF1' height='3' />
}
<ul>
<li><WaitingLink to={'/'}>Home Page</WaitingLink></li>
<li><WaitingLink to={'/somepage'}>Some Other Page</WaitingLink></li>
<li><WaitingLink to={'/somepage2'}>Some Other Page 2</WaitingLink></li>
</ul>
</header>
<Switch>
<Route exact path={'/'}>
<div style={{background: '#ff8888'}}>
<h1>Home Page</h1>
</div>
</Route>
<Route exact path={'/somepage'}>
<div style={{background: '#88ff88'}}>
<h1>Some Other Page</h1>
</div>
</Route>
<Route exact path={'/somepage2'}>
<div style={{background: '#8888ff'}}>
<h1>Some Other Page 2</h1>
{/*Попробуем данные из состояния взять, которые получены при клике*/}
{/*В реальности это может быть redux-srore*/}
<p>{data ? data : (getData(), null)}</p>
</div>
</Route>
</Switch>
</div>
);
};
When you create a reducer, you can set any initialState. Example in the doc :
import { VisibilityFilters } from './actions'
const initialState = {
visibilityFilter: VisibilityFilters.SHOW_ALL,
todos: []
}
function todoApp(state, action) {
if (typeof state === 'undefined') {
return initialState
}
// For now, don't handle any actions
// and just return the state given to us.
return state
}
The dude above is right, but you can leave it like this:
{
oneListOfObjects:[],
twoListOfObjects:[],
threeListOfObjects:[],
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question