Apologies if this is rudimentary, but I am not able to do a fairly simple task. I have a page where I have data rendered from an API into the cards like divisions. Ex:
<div class="card bg-light mb-3 offset-md-4" style={{ width: "30rem" }}>
<div class="card-body">
<h5 class="card-title">
{post.userDetails != undefined ? post.userDetails.name : ""}
</h5>
<p class="card-text">{post.emailAddress}</p>
<p class="card-text">
{post.userDetails != undefined ? post.userDetails.phone : ""}
</p>
</div>
</div>;
For each data item, there is a card displayed on screen. I am trying to find a way to click one of these cards and show the data on Another page where I can Edit/ Delete the selected card's data, but I have not been able to succeed in doing so. Below is my full code:
import React, { Component, useState, useEffect } from "react";
import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import axios from "axios";
import { URL } from "../Data/Env";
export default class UserPanel extends Component {
constructor(props) {
super(props);
this.state = {
post: [],
};
}
componentDidMount() {
this.getUser();
}
async getUser() {
let url = URL.concat("authors");
axios
.get(url)
.then((response) => {
console.log(response.data);
if (response.data.statusCode == 200) {
this.setState({ post: response.data.data });
} else if (response.data.statusCode == 400) {
console.log("User detail retrieving failed !!");
} else {
console.log("User detail unknown error !!");
}
})
.catch((error) => {});
}
showUser(post) {
return (
<div>
<Link
to={{ pathname: "/UserDetail", state: [{ userId: post.userId }] }}
>
<div
class="card bg-light mb-3 offset-md-4"
style={{ width: "30rem" }}
>
<div class="card-body">
<h5 class="card-title">
{post.userDetails != undefined ? post.userDetails.name : ""}
</h5>
<p class="card-text">{post.emailAddress}</p>
<p class="card-text">
{post.userDetails != undefined ? post.userDetails.phone : ""}
</p>
</div>
</div>
</Link>
</div>
);
}
render() {
const { post } = this.state;
return (
<div>
<br />
<br />
<div>
<div className="row col-md-12">
<div className="offset-md-4">
{" "}
<h3>All Users</h3>{" "}
</div>
<div>
<Link to={"/SignUp"}>
<button
className="btn btn-success"
style={{ marginLeft: "146px", width: "234px" }}
>
{" "}
Add User
</button>
</Link>
</div>
</div>
<br />
</div>
<div> {post.map(this.showUser)}</div>
</div>
);
}
}
Any help is really appreciated.
You shouldn't use
<Link>outside<Router>. First you need to create the routes then you can use<Link>to navigate to the relevant page. It's better to have separate components relevant to each route. Refer this link for basic routing examples from react-router: https://reactrouter.com/web/guides/quick-startFor the OP's scenario, it is essential to have separate
UsersandUserDetailcomponents, in whichUserscomponent shows all the users details andUserDetailcomponent shows selected user details.UserPanelmain component will be used to contain the routes of the application such as /UserDetail, /SignUp.Following implementation can be used as a supprotive material for OP's scenario. In this case, data fetching from api has been mocked for the easiness of understanding.
UserPanel.js
Users.js
UserDetail.js
Following are the application views for the relevant routes.
Route: /
Route: /UserDetail
In above implementation, each card's title (user name) has been linked to the /UserDetail route which shows user details. /SignUp route doesn't contain a component, but it can be routed to a particular child component as similar to
UsersandUserDetailcomponents.useLocationhook fromreact-router-domis used to get the location object relevant to the current URL. From it, location state can be extracted.Suggestions
Since data fetched from api is an array of objects, keep it stored as posts instead of post. Be careful about variable naming.
Avoid using functions such as
showUser(post)to introduce JSX elements into render method. Direclty introduce it into the component as follows.UsersandUserDetailcomponent. It's better to have separate child component asCardDetailsto reuse it)