score:9
Make this.state
such that it will control how your component gets rendered.
Now, within componentWillReceiveProps
, check the nextProps
argument
componentWillReceiveProps(nextProps, nextState) {
if( <check in nextProps if my route has changed> ) {
let newState = Object.assign({}, this.state);
// make necessary changes to the nextState Object by calling
// functions which would change the rendering of the current page
this.setState({ nextState });
}
}
This would make componentWillReceiveProps
take action only when the route changes.
Now in your render function,
render() {
const { necessary, variables, to, render } = this.state;
let renderVariables = this.utilityFunctionsReqToRender(someArgs);
return (
<toRenderJsx>
...
</toRenderJsx>
)
}
This would make your component "refresh" whenever the route changes.
score:1
componentWillReceiveProps is deprecated since React 16.3.0 (as says a warning in the browser console)
Reference : https://reactjs.org/docs/react-component.html#componentdidupdate
So componentDidUpdate can be used to get the new state and reload data depending on params
componentDidUpdate(prevProps, prevState, snapshot) {
console.log("componentDidUpdate " +prevState.id);
reloadData(prevState.id);
}
Source: stackoverflow.com
Related Query
- Using the same component for different Routes with React Router
- React Router switch does not mount same component for different routes
- Approach for different HTML structure with React and using the same components
- Two routes with different query strings for the same component
- route to different component for similar routes with react router
- How to render same react component for different routes with different value props?
- React.js generate dynamic routes for the same component with different data
- Using React Router with Switch and can't get the page to render different components when changing routes
- Create custom dynamic routes with react router and regex to the same component
- How can I use different properties for the same component using Styled Component for React JS?
- Is there a clean way to conditionally load and render different components for the same React Router route?
- React : How to stop re-rendering component or making an API calls on router change for the same route
- React Router trigger re-load / re-mount component on redirecting to same page with different slug
- React Router with different paths on same component: does not re-initialize component (useEffect [] not called)
- Using Styled Components with nested styles for React Search Bar, trying to place two different SVGs at opposite sides of the search bar
- React Router v6 : How to render multiple component inside and outside a div with the same path
- What's the correct way to render React components with different styles such as Navbar using React Router 6v?
- How to link to Routes inside the component rendered by another Route with React Router
- Syntactic Sugar for passing React props with the same key and value into component
- React route, use the same component with different props/parameters on CoreUI
- React Router Link to same component with different URL not re-rendering
- How I can create one component for different JSON files with the same structure
- React router 4 - Why is my component only rendered on manually setting the url but not on clicking a Link with same url?
- react-router v3: How to load data for the component when the same route with different query params is loaded
- Child route handled by the same parent's component using react router
- How can I create multiple instances of a React component (having an infinite animation) with different values for the :root variables
- React router with different templates for different routes v. 5
- Unable to use the same form component for Add or Edit Mode with React & Redux
- Multiple path names for a same component in React Router
- How to mock history.push with the new React Router Hooks using Jest
More Query from same tag
- Overflow X with max two rows
- update scroll position every 8 seconds with useState and useEffect?
- Chrome Extension: Refused to execute inline script, but no inline scripts present?
- In React/ES6, is there a more concise way to write a ternary that references an attribute?
- Recharts - pass array of values to Line component
- I must add login to navbar in React
- How do I recover the reset password token from Supabase in React?
- React-Native - i18n separate files
- How to remove \n \t in react
- How do I create a diagonal border in React Native?
- When should I use Lists vs Menus in Material-UI?
- Delete item from list not rendering react js
- Using NPM Modules in Namespaced Typescript project
- Function passed and invoked in child component doesn't have access to parent state value
- Cloudinary image upload from React: am including Cloudinary unsigned preset but get "Upload preset must be specified when using unsigned upload"
- How to get the reponse code from Web API in React
- React. preventDefault() for onCopy event does not work
- Why do React hooks use a const in the React Docs?
- What does this "react-scripts eject" command do?
- How do I display a table on the home page in React JS?
- How to add font-awesome-icon to React Component from json?
- ReactDnD, enable to create a simple DragDropContext - DragSource example
- Airbnb JS StyleGuide & history.push don't combine
- What is wrong with my checkAuth function?
- Edit NavBar information based on routing
- Why Enzyme not load style from React component?
- Framer-motion: onAnimation callbacks not called on child elements
- Get element position in the DOM on React DnD drop?
- Bootstrap Modal causes 'Uncaught TypeError: Cannot convert undefined or null to object' error in Heroku with Rails 6 app
- How to process onChange and change focus to new <Field>?