I'm fetching data from an API that gives me an array of objects. Each object contains some basic user info like first name, last name, email and so.
API response: [{first_name:"",...}, {first_name:"",...}, {first_name:"",...}]
After fetching the data from the API, I'm setting it to a state called 'data'
I'm rendering that array of objects through this code:
const renderData = () => {
var rows = [];
for (var i = 0; i < data.length; ++i) {
rows.push(
<tr key={i}>
<td>{i+1}</td>
<td>{data[i].first_name}</td>
<td>{data[i].last_name}</td>
<td>{data[i].email}</td>
<td>{data[i].states}</td>
<td>{data[i].city}</td>
<td>{data[i].pincode}</td>
<td className="action-btns">
<a className="edit-btn" href="/edit">EDIT</a> // Notice this line
<a className="delete-btn" href="/delete">DELETE</a>
</td>
</tr>
);
}
return (
<tbody>
{rows}
</tbody>
);
}
renderData gets called somewhere in the table:
export default MyComponent() {
return() {
<table>
<thead>...</thead>
{renderData()} // notice this line
</table>
}
}
Now you might have notice that I have EDIT and DELETE button/link, Whenever user clicks on EDIT we go to /edit url then we render a component using react router. I want to pass data to another component that takes a single user prop object, the component is a form which will prefill the data with the props provided. But how do I know which object EDIT button has been clicked
I tried something naive like:
<a onClick={() => setNumber(i)} className="edit-btn" href="#">EDIT</a> // Notice the onClick event
I thought if i is the value in the for loop, only and if I get to know which element of array in the data is been clicked then maybe I can pass it as a prop. I'm not sure how to do this. But using this method only gives me 11 which is the length of the array 'data' retrived from the API.
<Router>
<Switch>
<Route exact path="/" component={App} />
<Route path="/add" component={Add} />
<Route path="/edit" component={Add(props)} /> // How do I pass props here from Add.js to Main.js
</Switch>
</Router>