reactjsstring-length

ReactJS Uncaught TypeError: Cannot read properties of undefined (reading 'length')


I am making a striking application. I use json-server(fake api) as backend. So when i wanted to add students -list, i use this code: {User. length === 0 ? " " : alert('Checking')} so that I could check if it is working or not. Hovewer, I am getting an error. But on the other components, everything is fine. Idk what to do. pls help. Here is my code:

import * as React from "react";
import PropTypes from "prop-types";
import Tabs from "@mui/material/Tabs";
import Tab from "@mui/material/Tab";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
import { useEffect } from "react";
import axios from "axios";
import { useState } from "react";

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`vertical-tabpanel-${index}`}
      aria-labelledby={`vertical-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box sx={{ p: 3 }}>
          <Typography>{children}</Typography>
        </Box>
      )}
    </div>
  );
}

TabPanel.propTypes = {
  children: PropTypes.node,
  index: PropTypes.number.isRequired,
  value: PropTypes.number.isRequired,
};

function a11yProps(index) {
  return {
    id: `vertical-tab-${index}`,
    "aria-controls": `vertical-tabpanel-${index}`,
  };
}

export default function StudentsList() {
  const [value, setValue] = React.useState(0);

  const handleChange = (event, newValue) => {
    setValue(newValue);
  };

  const [User, setUser] = useState();

  useEffect(()=>{
    axios.get('http://localhost:3001/users').then(res=>setUser(res.data))
  },[])
  return (
    <Box
      sx={{
        flexGrow: 1,
        bgcolor: "background.paper",
        display: "flex",
        height: 224,
      }}
    >
      <Tabs
        orientation="vertical"
        variant="scrollable"
        value={value}
        onChange={handleChange}
        aria-label="Vertical tabs example"
        sx={{ borderRight: 1, borderColor: "divider" }}
      >
        <Tab label="Item One" {...a11yProps(0)} />
        <Tab label="Item Two" {...a11yProps(1)} />
        <Tab label="Item Three" {...a11yProps(2)} />
      </Tabs>
      <TabPanel value={value} index={0}>
        {User.length === 0 ? " " : alert('a')}
      </TabPanel>
      <TabPanel value={value} index={1}>
        Item Two
      </TabPanel>
      <TabPanel value={value} index={2}>
        Item Three
      </TabPanel>
    </Box>
  );
}

I hope smn could help me with that problem. Thanks for ur attention


Solution

  • Well, your User is initialized as:

    const [User, setUser] = useState();
    

    Please notice that it is filled with nothing, and nothing doesn't have a length attribute, there's no utility for it. You should initialize the state as:

    const [User, setUser] = useState([]);
    

    as an empty array, so the length is 0, or, like this:

    const [User, setUser] = useState("");
    

    as a string, by definition an array of characters.