【发布时间】:2022-01-12 12:13:21
【问题描述】:
我想将数组 inputArrival 和 inputBurst 发送到子组件 Barchart.js。但是每当我这样做时,只会发送我输入的第一个元素,而不是其他项目。基本上我想要的是,当数据被输入到 Entrytable.js 时,它会立即得到反映。 EntryTable.js
import React,{useState} from 'react';
import './tableEdit.css';
import Barchart from './Barchart';
const EntryTable = (props) => {
const entry = props.numOfEntries;
const [inputArrival, setInputArrival] = useState(Array(entry).fill(""));
const [inputBurst, setInputBurst] = useState(Array(entry).fill(""));
function changeArrival(index) {
return (e) => {
setInputArrival((values) =>
values.map((value, i) => (i === index ? e.target.value : value)));};
}
function changeBurst(index) {
return (e) => {
setInputBurst((values) =>
values.map((value, i) => (i === index ? e.target.value : value))
);
};
}
const ArrayEntry = Array.from({ length: entry}).map((_, i) => (
<tr key={i}>
<td >P{i}</td>
<td >
<input
type="number"
value={inputArrival[i]}
onChange={changeArrival(i)}
/>
ms
</td>
<td >
<input
type="number"
value={inputBurst[i]}
onChange={changeBurst(i)}
/>
ms
</td>
</tr>
));
return (
<div c>
<table>
<thead>
<tr>
<th>Process</th>
<th>Arrival Time</th>
<th>Burst Time</th>
</tr>
</thead>
<tbody>{ArrayEntry}</tbody>
</table>
<button>Click Me</button>
<Barchart selected_val={entry} arrivalArray={inputArrival} ></Barchart>
</div>
);
};
export default EntryTable
Barchart.js
import React,{useState,useEffect} from 'react'
const Barchart=(props)=>{
const sel = props.selected_val;
var ar1=props.arrivalArray;
console.log(ar1);
}export default Barchart;
【问题讨论】:
-
首先,将
var替换为let或const -
您能否提供一个最小且可重现的示例? stackoverflow.com/help/minimal-reproducible-example
-
我已经尽力减少代码了。
标签: javascript html reactjs react-hooks use-state