Reputation: 813
Here's the workflow:
Here's the problem:
When I consume the drop down change event that's fired after selection, it contains the new flavor. However, it doesn't tell me the record from the table that changed (which I need). I need this to update the record in the backend.
Here's the code:
...
<tbody>
{data.map((row, key) => (
<tr key={key}
<td>
<select onChange={handleStatusChange} className="form-select">
<option value="value">{myObject[key].flavor}</option>
<option value="chocolate">chocolate</option>
<option value="strawberry">strawberry</option>
<option value="Vanilla">Vanilla</option>
</td>
...
The weird thing is that I can obviously identify row and row id from "row/key" within the table. However, whenever I consume the event, none of that data is there:
const handleStatusChange = (event, id:any) => {
backendService.setNewFlavor(data).then(response) => {
...this needs row.flavorID
}
}
What I've tried
Manually getting the row index with event.target.parentElement.rowIndex and seeing if I can do anything with that, but it says that's undefined (and idk if I'd be able to do anything with it anyway).
Looking at examples in other applications (most seem to use MUI to handle event listeners).
Trying to invoke the data element passed into the component (it just says it's null). That component looks like this:
const IceCreamTable = ( {data} ) => {
const handleStatusChange = (event, id:any) => {...}
return (
...table from previous code snippet
)
}
I don't need you to spoonfeed me or solve the problem for me, I just need an example of someone doing something like this.
Bonus points if we can make this change dynamically render to the page without refreshing it.
Upvotes: 0
Views: 866
Reputation: 218877
If key
(which is just the array index) really is the "unique identifier" you need then you can simply pass it to the handler function:
onChange={e => handleStatusChange(e, key)}
Then the function signature you have would receive those two arguments:
const handleStatusChange = (event, id:any) => {
Alternatively, if row
itself contains an identifier you can use, you can pass that to the handler:
onChange={e => handleStatusChange(e, row.id)}
Or pass the entire row
itself:
onChange={e => handleStatusChange(e, row)}
Basically, you can pass whatever argument(s) you like to handleStatusChange
.
Upvotes: 1