How to dispatch Redux action from stateless component when route is loaded?

I don’t know why you absolutly want a stateless component, while a stateful component with componentDidMount would do the job in a simple way. Dispatching actions in mapDispatchToProps is very dangerous and may lead to dispatching not only on mount but whenever ownProps or store props changes. Side effects are not expected to be done … Read more

getting 404 for links with create-react-app deployed to github pages

Problem: URL gets evaluated on server side When you enter a new URL into address bar of the browser or refreshes the page, browser requests server (in this case GitHub pages server) for that URL. At this point, client side router (react-router) can’t take action as it is not yet loaded for that page. Now … Read more

React Router work on reload, but not when clicking on a link

I would go through your components and make sure you have only one <Router> … </Router>. Also — make sure you have a <Router>…</Router> There may be cases when you’d use more than one, but if you accidentally have nested routers (because you were hacking quickly and forgot to remove one when you were moving … Read more

react-router-dom with TypeScript

I use a different approach to fix this. I always separate the different properties (router, regular and dispatch), so I define the following interfaces for my component: interface HomeRouterProps { title: string; // This one is coming from the router } interface HomeProps extends RouteComponentProps<HomeRouterProps> { // Add your regular properties here } interface HomeDispatchProps … Read more

React Router: Cannot read property ‘pathname’ of undefined

I got the above error message and it was extremely cryptic. I tried the other solutions mentioned and it didn’t work. Turns out I accidentally forgot to include the to prop in one of the <Link /> components. Wish the error message was better. A simple required prop “to” not found or something like that … Read more

How to sync Redux state and url query params

I would suggest just using React Router directly and not keeping searchState in Redux. React Router will inject URL parameters into your components, and you can use them in mapStateToProps(state, ownProps) to calculate the final props. If you really want to see route changes as actions, you can use react-router-redux for two-way syncing, but it … Read more

Detect Route Change with react-router

You can make use of history.listen() function when trying to detect the route change. Considering you are using react-router v4, wrap your component with withRouter HOC to get access to the history prop. history.listen() returns an unlisten function. You’d use this to unregister from listening. You can configure your routes like index.js ReactDOM.render( <BrowserRouter> <AppContainer> … Read more

react-router scroll to top on every transition

but classes are so 2018 ScrollToTop implementation with React Hooks ScrollToTop.js import { useEffect } from ‘react’; import { withRouter } from ‘react-router-dom’; function ScrollToTop({ history }) { useEffect(() => { const unlisten = history.listen(() => { window.scrollTo(0, 0); }); return () => { unlisten(); } }, []); return (null); } export default withRouter(ScrollToTop); Usage: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)