Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
React State Hook initialised to [] still shows type as undefined

Code:

const [input, setInput] = useState('');
const [studentList, setStudentList] = useState([]);

useEffect(() => {
  console.log(studentList)
  console.log(studentList.type)
}, [studentList]);

return (
  <div id="add-students-input-div">
    <input
      type="text"
      id='add-students-input'
      value={input}
      placeholder='Enter a student to the database'
      onChange={(event) => {
        setInput(event.target.value)
      }}
    />
    <div id="add-students-button" onClick={() => {
      setStudentList([document.getElementById('add-students-input').value, ...studentList])
      setInput('')
     }}>
      <p>Add</p>
    </div>
  </div>
)

Problem: The print statement for studentList is returning the array but the print statement for studentList.type is undefined at all times, even after elements are added to the array.

How can I ensure that studentList.type returns Array.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

studentList in your code will ever be an array, empty when you initialize the state. If you want to check if there is something in the array you have to use studentList.length

about 4 years ago · Santiago Trujillo Report

0

As mentioned in the comments, arrays do not have a type property.

Your studentList state value is always an array; there is no need to check its type.

One thing you do appear to be doing incorrectly is updating studentList when you click your button (<div>). In short, you really shouldn't need to use DOM methods in React.

To update your array with the value from your input state, use this...

const handleClick = () => {
  setStudentList((prev) => [input, ...prev]);
  setInput("");
};

and

<div id="add-students-button" onClick={handleClick}>
  <p>Add</p>
</div>

See useState - Functional updates for information on how I'm using setStudentList()

about 4 years ago · Santiago Trujillo Report

0

Altough previous contributors solved your problem by eliminating it in other place, I would like to answer this:

How can I ensure that studentList.type returns Array

If you want to make sure your variable is an array, you may use isArray() static method of Array:

Array.isArray(studentList) // returns true if array or false if not
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!