【发布时间】:2021-04-21 05:07:42
【问题描述】:
我正在尝试使用 State 将输入更改为“年份”时显示年份,但我遇到了问题。
const handleChange = (e, countMonth,countYear) =>{
if(e.target.value === 'months'){
setMonths(countMonth)
} else if( e.target.value === 'year'){
setMonths(countYear)
}
}
我正在尝试使用此地图方法单击年份时显示不同的数据:
<tbody>
{
months.map(({ date, count }, index) => (
<tr key={index}>
<td>{date}</td>
<td>{count}</td>
</tr>
))}
</tbody>
我不断收到错误'TypeError: Cannot read property 'map' of undefined'
这里是完整的沙盒代码:https://codesandbox.io/s/wizardly-clarke-szge3?file=/src/data.js
【问题讨论】:
-
这里是你调用
handleChange的方式,它需要3个参数~onChange={(e) => {handleChange(e)}}。看到问题了吗? -
您的
handleChange处理程序需要 3 个参数,但您只传递第一个。countMonth和countYear不在我所知的范围内。 -
是的,我尝试将这两个添加为参数,但仍然不起作用
标签: javascript reactjs nivo-react