首页 > 解决方案 > 当我在反应 js 中尝试钩子时,状态没有更新?

问题描述

我是反应js的初学者。当我尝试钩子时,状态没有更新

使用 useState 定义状态,如下所示:

 const [customerParams, setCustomerParams] = useState({
    name: '',
  });

然后我必须从 onFilterChange 函数中获取值(使用 mui 数据表过滤更改)。我必须用选定的值更新状态。这是我的 onFilterChange 函数:

     onFilterChange: (changedColumn, filterList, type) => {
    switch (changedColumn) {
      case 'Name':
        {
          if (filterList[0].length > 0) {
            const name = filterList[0].toString();
            setCustomerParams({ ...customerParams, name: name });
          }
        }
        break;

    }
  },

这里changedColoumn、filterList、type都是通过点击filter得到的,filterList[0]是name的值。我必须用选定的名称更新状态。但不更新请帮帮我

标签: reactjsreact-hooksmui-datatable

解决方案


推荐阅读