This is an index page.
import React from "react";
import { render } from "react-dom";
import { BrowserRouter } from 'react-router-dom';
import {Router, Route, browserHistory} from "react-router";
import {EmployeeList} from "./employeeList";
import {EditForm} from "./edit";
class App extends React. Component{
render(){
return (
<BrowserRouter history={browserHistory}>
<switch>
<Route path={"/"} component={EmployeeList} />
<Route path={"edit/:id"} component={EditForm} />
</switch>
</BrowserRouter>
);
}
}
render(<App />, window.document.getElementById("app"));
this is an employee page from this page..after clicking button i want fetch id and need to pass to edit js page where i need call anthor api with this fetched id.
import React from "react";
import {browserHistory} from "react-router";
import { createHashHistory } from 'history';
export class EmployeeList extends React. Component {
constructor(props) {
super(props);
this.state = {
students: [],
}
this.onNavigateEdit = this.onNavigateEdit.bind(this);
};
onNavigateEdit() {
//alert(id);
this.browserHistory.push("/edit");
}
componentDidMount() {
fetch('https://api.myjson.com/bins/bz92u')
.then(response => response.json())
.then((responseJson) => {
this.setState({ students: responseJson.students })
})
}
render() {
const student = this.state.students.map((item, i) => (
<tr>
<td>{item.id}</td>
<td>{item.fname}</td>
<td>{item.lname}</td>
<td>
<button className="btn btn-primary" onClick={this.onNavigateEdit}>
EDIT
</button>
</td>
</tr>
));
return (
<div>
<table class="table table-dark table-striped">
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Button</th>
</tr>
</thead>
{student}
</table>
</div>
);
}
}
this is an edit js page.
import React from "react";
import { render } from "react-dom";
export class EditForm extends React. Component {
constructor(props) {
super(props);
this.state = {
students: []
}
};
render() {
return (
<div></div>
);
}
}
M getting type error is Cannot read property 'push' of undefined. so m consfused with white router method need to be use.