简体   繁体   English

useState 使用新的 Array 方法定义一个空数组

[英]useState define an empty array with new Array method

I am getting data which is an array of objects from the store.我正在从商店获取数据,它是一组对象。 In useState I want to set an array of data.length size all set to false initially.在 useState 中,我想设置一个 data.length 大小的数组,最初都设置为 false。 But when I set the value it returns an empty array [] for each of the state variable I set.但是,当我设置该值时,它会为我设置的每个 state 变量返回一个空数组 []。 I also tried updating the state in the useeffect but nothing works.我还尝试在useeffect中更新state,但没有任何效果。 I am unable to figure out what is the issue here.我无法弄清楚这里有什么问题。

function Datatable() {
       let data = useSelector((state) => state.dish);
      console.log(data.length)
      const [clicked1, setClicked1] = useState(new Array(data.length).fill(false));
      const [clicked2, setClicked2] = useState(new Array(data.length).fill(false));
      const [clicked3, setClicked3] = useState(new Array(data.length).fill(false));
      const dispatch = useDispatch();
      function setAllStates() {
        setClicked1(new Array(data.length).fill(false));
        setClicked2(new Array(data.length).fill(false));
        setClicked3(new Array(data.length).fill(false));
      }
      useEffect(() => {
        setAllStates();
      }, []);

image of console the data Here is my jsx where i am creating the table控制台数据的图像这是我创建表的 jsx

<TableBody>
            {data.map((row, index) => (
              <TableRow
                key={row.id}
                sx={{ "&:last-child td, &:last-child th": { border: 0 } }}
              >
                <TableCell component="th" scope="row">
                  <img width={150} height={100} src={row.image} />
                </TableCell>
                <TableCell align="right">{row.dishName}</TableCell>
                <TableCell align="right">
                  {clicked1[index] == false ? (
                    <>
                      <Button
                        onClick={() => handleOnclick("Rank 1", index, row)}
                      >
                        Rank 1
                      </Button>
                    </>
                  ) : (
                    <Typography>Selected</Typography>
                  )}
                </TableCell>

Here is the image of the table in console data.length is showing 0 two times and then 30. I am populating the data in the reducers in its parent component.这是控制台数据中表格的图像。长度显示 0 两次,然后显示 30。我在其父组件的减速器中填充数据。 But still all the arrays are undefined.但是所有的 arrays 仍然是未定义的。 And in the table all i am showing in the data table are undefined.在表格中,我在数据表中显示的所有内容都是未定义的。 (Note: I am creating a table of length data.length. data.image, data.description are showing in the table only the buttons that are showing only when clicked1[index] == false are not defined. (注意:我正在创建一个长度为 data.length.data.image 的表,data.description 仅在表中显示,仅在 clicked1[index] == false 时才显示的按钮未定义。

Unless data.length is 0, this gotta work.除非data.length为 0,否则这必须工作。

    function Datatable() {
          const data = useSelector((state) => state.dish);

          const [clicked1, setClicked1] = useState();
          const [clicked2, setClicked2] = useState();
          const [clicked3, setClicked3] = useState();

          const dispatch = useDispatch();

          function setAllStates(size) {
            const newArray = new Array(size).fill(false);
            setClicked1(newArray);
            setClicked2(newArray);
            setClicked3(newArray);
          }

          useEffect(() => {
            if(data) setAllStates(data.length);
          }, [data]);
     }

Your code seems fine.你的代码看起来不错。

Possible bug may be -> data.length being 0.可能的错误可能是 -> data.length 为 0。

To check add console.log(data.length) after data is set.在设置data后检查添加console.log(data.length)

Now the code:现在代码:

let data = useSelector((state) => state.dish);
console.log(data.length) // if this is zero , this is causing empty array
const [clicked1, setClicked1] = useState(new Array(data.length).fill(false));
...

Edit:编辑:

Add data as dependency.添加data作为依赖项。

 useEffect(() => {
        setAllStates();
      }, [data]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM