0

I cannot use array methods on an array of objects. The shellsArray logs out (6) [{…}, {…}, {…}, {…}, {…}, {…}] in the console, but I am unable to use array methods on it. The proto of the console says array(0)

    import React, { useState } from 'react'

const RecipeForm = (props) => {
    const [baseLayer, setBaseLayer] = useState('');
    const [condiment, setCondiment] = useState('');
    const [mixing, setMixing] = useState('');
    const [seasoning, setSeasoning] = useState('');
    const [shell, setShell] = useState('');
**let tacos = props.tacos;
    let shellsArray = tacos.shells;** // Shows an array that I cannot use array methods on
    console.log(shellsArray);

    const submitHandler = (e) => {
        e.preventDefault();
        props.saveCombination(baseLayer, condiment, mixing, seasoning, shell);
        setBaseLayer('');
        setCondiment('');
        setMixing('');
        setSeasoning('');
        setShell('');
    }
    return (
        <form onSubmit={submitHandler} >
            <select className="base-layer" name="baseLayer" value={baseLayer} onChange={(e) => setBaseLayer(e.target.value)} required >
            <option>SELECT BASE LAYER</option>
            <option>Mexican</option>
            <option>Asian</option>
            </select>
            <select className="condiment" name="condiment" value={condiment} onChange={(e) => setCondiment(e.target.value)} required >
            <option>SELECT CONDIMENT</option>
            <option>Tomato</option>
            <option>Lettuce</option>
            </select>
            <select className="mixing" name="mixing" value={mixing} onChange={(e) => setMixing(e.target.value)} required >
            <option>SELECT MIXING</option>
            <option>Tomato</option>
            <option>Lettuce</option>
            </select>
            <select className="seasoning" name="seasoning" value={seasoning} onChange={(e) => setSeasoning(e.target.value)} required >
            <option>SELECT SEASONING</option>
            <option>Tomato</option>
            <option>Lettuce</option>
            </select>
            <select className="shell" name="shell" value={shell} onChange={(e) => setShell(e.target.value)} required >
            <option>SELECT SHELL</option>
            <option>Tomato</option>
            <option>Lettuce</option>
            </select>
            <input type="submit" className="submit" />
        </form>
    );
}

export default RecipeForm;
10
  • 1
    what methods are not working? and what is the error Commented Oct 10, 2019 at 11:54
  • 1
    You completely omit where your props are set. I also don't see any code using shellsArray other than the console.log. Commented Oct 10, 2019 at 11:55
  • "The proto of the console says array(0)" this means that the prototype is the Array constructor function, so you should be able to call array methods. What happens if you do? Commented Oct 10, 2019 at 11:58
  • I am trying to use foreach(), but when I use this React logs TypeError: Cannot read property 'forEach' of undefined Commented Oct 10, 2019 at 11:58
  • How are you calling that? Can you show the code? Commented Oct 10, 2019 at 11:59

1 Answer 1

1

If your shells array is a prop, it looks like you're trying to use it before the prop is defined. This is a common situation, and the usual approach it to guard against the error by first testing if your array is defined or not. The easiest method is to use a shortcut conditional:

Array.isArray(shells) && shells.map(shell=>{}) //or whatever
Sign up to request clarification or add additional context in comments.

1 Comment

This was the solution to my problem, thank you very much for resolving it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.