score:1
One way of going about it is to add a componentDidUpdate
lifecycle hook to the SearchPage
component and check if the term
parameter changes and do the search again.
If you want to remount the component when the term
parameter changes instead, you can use the key
prop to unmount the previous component and mount a new one when the key
changes.
<Route
path={"/search/:term"}
render={props => <SearchPage key={props.match.params.term} {...props} />}
/>
score:1
Use the Link
from react-router-dom
instead of the <button>
to change the route instead of just redirecting. Then in the onClick of the <Link>
component call the fetchData
and set the state accordingly.
<Link className="btn" to={"/search/"+this.state.text} onClick={this.handleOnClick}>Search!</Link>
Then in the handleOnClick
function
handleOnClick() {
this.setState({
loading: true
});
fetchData(this.state.text).then(jsonResponse => {
this.setState({
loading: false,
data: jsonResponse
});
});
}
Source: stackoverflow.com
Related Query
- React Router trigger re-load / re-mount component on redirecting to same page with different slug
- React Router doesn't load the new page component after redirecting via navigate
- React router render component on same page
- React Router switch does not mount same component for different routes
- React Router "Link to" does not load new data when called from inside the same component
- React router doesn't load page from other component page
- react router v4 redirecting to another page outside of component
- React Router | load component only on page refresh
- How to make a nested react router component load to a new page and not half-way down the page
- Load same configuration after component is re-rendered or the page is refreshed in react
- React router how to display each component on click on the same page
- Multiple path names for a same component in React Router
- React Router v4: Cant load page on GitHub pages
- React Router - Stay at the same page after refresh
- React router v4 history.push to the same route (different query params) in Functional Component (use Hooks API)
- Is there a clean way to conditionally load and render different components for the same React Router route?
- React Router - Go back to the last page visited, and not the default component
- React router is not reloading page when url matches the same route
- React router - pass api data to the linked component to open with a new page
- React : How to stop re-rendering component or making an API calls on router change for the same route
- Redirecting using React Router shows blank page
- React Router - 404 page always showing due to custom route component
- how to update a route in React Router without re-mounting the component in a single page app?
- Using the same component for different Routes with React Router
- React Router master-detail: children component don't render new view until page refresh
- React Router useHistory. History.push changes url but does not load component
- React Router Still Renders Previous Page after Routing to New Component
- React router redirects the page but component doesn't get rendered
- React Router v4: Component and Children in the same route
- React Router Load Any of One Component in else
More Query from same tag
- Calculating "Duration" on overall workout time
- ReactDOM.render not accepting JSX
- password field only display only after user name entered?
- history.push() does not work on the same page using react
- State update not reflecting in custom hook
- How to fetch a list of objects and wait for them to finish?
- Memory heap increases when Chrome tab is inactive
- How to in GraphQL mutation pass an array to a field using FaunaDB
- on clicking button, change classname of the clicked button as well as other buttons in that group using reactjs
- React - change input defaultValue by passing props
- Jquery onClick function is undefined in React
- I get no image when dynamically inserting a url in jss for backgroundImage. React, material-ui
- Cannot read property 'map' of undefined in MenuComponent
- How to override antd select option hover?
- How to redirect to another page if user gets to a specific URL in Next.js?
- Implementing Redux to React to return JSON data from provided endpoint
- How to fix the error type Item[] | undefined is not assignable to Item[] type using react and typescript?
- emitting action with socketio into different react components
- React-router: How nested routes work?
- Disable Minus Button when Quantity is equal to 1.
- How do I mock a const method IN a component as part of an integration test?
- Material UI hide element onClick or when checkbox is checked
- Django/React - Static Main.js file with AWS hosting staticfiles
- React passing onClick as props to sub components required?
- Importing/fetching data in functional component slowing page load
- How to fix self is not defined when firebase messaging method called in react ssr app?
- Dynamic form handling in react
- after deletion the UI re renders and getting all the check boxes
- How do I run some config before every Jest test run
- How to use React-router with {this.props.children}