I am making simple employee list and detail demo into ReactJS. I have employee list component which shows list of all employee fetch from some dummy rest web service and shows on page.
class contactList extends Component {
constructor(props) {
super(props);
this.state = {
employees: [],
};
}
componentDidMount() {
this.EmployeeList();
}
EmployeeList() {
fetch('https://jsonplaceholder.typicode.com/comments')
.then(response => response.json())
.then(results => {
this.setState({ employees: results })
.catch(
error => { console.log(error); }
);
});
}
onDelete(e) {
}
render() {
console.log(this.state.employees);
const persons = this.state.employees.map((item, i) => (
<div>
<table>
<tbody>
<tr>
<td>
<Link to={"/Contactdetails/" + item.id} >{item.name}</Link>
<Route exact path={"/Contactdetails/:id"} component={Contactdetails}></Route>
</td>
<td>
<button onClick={this.onDelete}>
Delete
</button>
</td>
</tr>
</tbody>
</table>
{/* <Link to="/">Delete</Link> */}
</div>
));
return (
<div id="layout-content" className="layout-content-wrapper">
<div className="panel-list">{persons}</div>
</div>
);
}
I've shown all a tags with corresponding id(parameter) on the page. Issue when I click on link, it doesn't take me to detail page. In detail page, I kept alert as well in render method, but it is also not appearing, So, I think detailpage is not getting call.
class Contactdetails extends Component {
constructor(props) {
super(props);
this.state = {
};
}
componentWillMount(){
alert("Helloo");
}
componentDidMount() {
console.log("props: ", this.props.match)
this.EmployeeDetails();
}
EmployeeDetails() {
fetch('http://dummy.restapiexample.com/api/v1/employees')
.then(response => response.json())
.then(results => { this.setState({ employees: results }) });
}
render() {
alert("Hello");
return (
<h2>Contact Details</h2>
)
}
};
StackBlitz link: https://stackblitz.com/edit/react-f4eha2?file=Contactdetails.js