score:0
I think you need react-router-dom
I recommend you learn it. Below is my implementation with router.
App.js
import {BrowserRouter as Router, Switch, Route} from "react-router-dom"
import Cars from "./Cars.js" // Change it to the correct path
import InfoCard from "./InforCard.js"
class App extends React.Component {
render() {
<Router>
<Switch>
<Route path="/cars" component={Cars}></Route>
<Route path="/cars/:id" component={InfoCard} exact></Router>
<Switch>
</Router>
}
}
export default App;
Cars.js
class Cars extends React.Component {
constructor(props) {
super(props);
this.state = {
carData: null,
checkData: false
}
}
getData = () => {
fetch('http://localhost:8000/api/all-articles')
.then( (response) => response.json() )
.then( (response) => {this.setState({
carData : response,
checkData : true
})});
}
componentDidMount = () => {
this.getData();
}
displayCars = () => {
return(
this.state.carData.data.map( (object, index) => (
<CarCard key={index} name = {object.title} description={object.description} img={object.image} id={object.id}/>
) )
);
}
render() {
if(this.state.checkData){
return(
<div>
<Title/>
<div className="cars">
{this.displayCars()}
</div>
</div>
);
}else {
return(
<div>Loading..</div>
);
}
}
}
export default Cars;
CarCard.js
import { withRouter } from "react-router-dom"
class CarCard extends React.Component {
render() {
console.log(this.goToCardInfo(this.props.id));
return(
<div className="card">
<Card>
<Card.Img variant="top" src = {`http://localhost:8000/images/${this.props.img}`}/>
<Card.Body>
<Card.Title>{this.props.name}</Card.Title>
<Card.Text>
{this.props.description}
</Card.Text>
<Button variant="primary"
onClick={() => this.props.history.push("/card/" + this.props.id)}>See announce</Button>
</Card.Body>
</Card>
</div>
);
}
}
export default withRouter(CarCard);
InfoCard.js
class InfoCard extends React.Component {
// you can get the id of the card like this
const url = window.location.hash;
const id = url.substring(url.lastIndexOf("/") + 1);
// use that id to query your car
render() {
return(
<h2>hello</h2>
);
}
}
export default InfoCard;
Source: stackoverflow.com
Related Query
- Click on a card and then navigate to card page with details ,in react
- React router - click on card and redirect to a new page with content of that card (card details) using useParams and reacthooks
- React router - click on card and redirect to a new page with content of that card (card details)
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- How to fetch data on page load and then on button click with useEffect
- I'm working with React Route, and I have a random string, but when I do click on the link page works but when a do click again doesn't work
- How to click a button and navigate to a new route with URL params using React Router 4
- React - Triggering click event on table row and display the selected row details in another page
- Navigate to a new page with React when I click on a button
- React - How do I start a timer, then navigate a different route, then come back and have the timer still in sync with its original countdown?
- React router - click on Productcard and redirect to a component with details of that clicked card,
- Proper way to navigate with React Native, Redux, and Navigator
- React table dynamic page size but with size limit and pagination
- How to Navigate and Scroll to an Element with ID in a Next.js Page wrapped with AnimatePresence
- How to nest Scene and navigate with direction='vertical' in React Native Router Flux?
- Why is my React component render called twice, once without data and then later with data, but too late exception?
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- React Redux with redux-observable use the router to navigate to a different page after async action complete
- Change state on click then change again after delay and show message in React
- How to setup sublime 3 with react native so that when you click on the error and it jump to your code?
- React with Material-ui card media, add overlay on click
- Change page background color with each route using ReactJS and React Router?
- Navigate to next page on Table row click in React JS
- Conditionally check value from context and then navigate after login in the same function call in React
- How to Create a dropdown Button with see more option and on click toggle should not happen in react js?
- mongoose-paginate with react and material-ui TablePagination - second page onwards not rendering after clicking of onChangePage prop
- React, how to pass product id to details page with React Router Dom?
- Navigate to other page with props in React JS
- How to call a function and then navigate to a route when click on a button?
- I have this `div` on my home page and want it to move with me when I scroll, I am using react for the website btw
More Query from same tag
- use of conventional Javascript classes for models in React
- React.js Read User Info from AWS and set variable state
- 404 (Not Found) when attempting to POST information to heroku
- Material-UI 5 DataGrid styles are not isolated between components
- Updating component state inside of useEffect
- Inferences from JSX vs calling React Components as functions
- Element is removed from document, but jest still finds it
- clearInterval does not reset
- What data structure should I use if these data would need prices for each?
- Material UI disable checkbox hover
- React final form not working with typescript
- React Datepicker is sending value as string insteadl of YYYY-mm-dd
- react scrolling a div by dragging the mouse?
- Unhandled Rejection (TypeError): illegal operation attempted on a revoked proxy
- React Redux doesn't return expected state
- Is direct map in the useState hook an anti-pattern?
- Jest test hasClass() method returns false even when it is true
- How to get selected value of a dropdown menu in ReactJS
- react-route,react-hot-loader.webpack (You cannot change <Router routes>; it will be ignored)
- ReactJS - replace / expire date card
- update to hook causes Invalid attempt to destructure non-iterable instance
- Data coming back as undefined in react
- React typescript test on enzyme doesn't simulate click
- Expected argument to be of type `array` but received type `string` - image-webpack-loader
- How to redirect pages/app folder to subdomain in next.js
- How to add examples a component with dependencies in react-styleguidist
- How to reach Chart.helpers.color in a React context with chart.js 3.0.0?
- Bing Maps in React JS - Component doesn't Load when props change
- Asset images from Contentful CMS reach my Gatsby/React app, but a browser error says they are "null"
- smooth scrolling with a bootstrap navbar in react js