Javascript 在显示表数据时,继续获取“未定义的映射”,然后

Javascript 在显示表数据时,继续获取“未定义的映射”,然后,javascript,reactjs,nivo-react,Javascript,Reactjs,Nivo React,我试图在使用状态将输入更改为“年”时显示年,但我遇到了一个问题 const handleChange = (e, countMonth,countYear) =>{ if(e.target.value === 'months'){ setMonths(countMonth) } else if( e.target.value === 'year'){ setMonths(countYear) } } 使用此映射方法单击年份时,我尝试

我试图在使用状态将输入更改为“年”时显示年,但我遇到了一个问题

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:无法读取未定义的”的属性“map”


下面是完整的沙盒代码:

在您的沙盒代码中,我尝试放置控制台日志,发现countYear未定义,并且设置为months

理想情况下,您应该在countYear设置为state months之前对其进行验证[如果e.target.value==='year'&&countYear],或者修复countYear未定义的根本原因


下面是调用handleChange的方法,它需要3个参数~onChange={e=>{handleChange}}。看到问题了吗?handleChange处理程序需要3个参数,但只传递第一个参数。countMonth和countYear不在我能说的范围内。是的,我尝试添加这两个参数,但仍然不起作用。回答的目的不是帮助修复countYear未定义的根本原因,而不是仅仅指出代码不起作用?这是一个怎样有用的答案?你能把它放进sanbox请愿书吗?
const handleChange = (e, countMonth,countYear) =>{
      
  if(e.target.value === 'months'){
    console.log('countMonth ', countMonth)
    setMonths(countMonth)

  } else if( e.target.value === 'year'){
    console.log('countYear ', countYear)
    setMonths(countYear)
  
  }
}