// dianamic variable for accepting table values
const [revChange,setRevChange]=useState({})
const rcTableRows=5; // from user
const no_rows=[];
for(let i=1;i<=rcTableRows;i++)
{
let key=`r${i}`;
revChange[key]={
revision:"",
date:"",
reason:"",
remarks:""};
no_rows.push(`r${i}`)
}
<tbody>
{
no_rows.map((res,index)=>(
<tr>
<td class="border border-slate-200 ...">{index+1}</td>
<td className="border border-slate-200">
<input
type="text"
placeholder="rev"
onChange={(e) => {
setRevChange(prevData => ({
...prevData,
[res]: {
...prevData[res],
[name]: value // Update the specific field (e.g., revision)
}
}))
}}
name="revision" // Optional: Use 'name' attribute for field identification
/>
</td>
<td class="border border-slate-200 ...">Date</td>
<td class="border border-slate-200 ...">Reason for changes</td>
<td class="border border-slate-200 ...">Remarks</td>
</tr>
))
}
</tbody>
How can I create a dynamic variable with respect to my table row? Here I am providing a small React code snippet. I need to know how I can implement that logic in my given code.
I can’t accept data from my input field to the given dynamic variable. Is there any solution?
New contributor
Nihaliya Nichu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.