Skip to content
Advertisement

React Hooks: Passing state prop is NOT a function?

I am passing state hooks as props to my components and my ‘setCity’ works perfectly fine but when I try implementing setFlag I get an error saying “Searchbar.jsx:15 Uncaught TypeError: setFlag is not a function”

So why can I set the value of city but not flag? I pass the props from ‘App’ to ‘footer’ which passes to ‘searchbar’.

function App(){
  const [city, setCity] = useState("");
  const [flag, setFlag] = useState("");
  return (
    <div className='container'>
      <Header className='header' />
      <Body className='body' city={city} flag={flag}/>
      <Footer className='footer' setCity={setCity} setFlag={setFlag}/>
    </div>
  );
}
function Footer({setCity}, {setFlag}){
    return(
        <div className='footer'>
            Footer
            <Searchbar className='searchbar' setCity={setCity} setFlag={setFlag}/>
        </div>
    )
}
function Searchbar({setCity}, {setFlag}){

    handleChange = (e) =>{
        e.preventDefault();
        console.log(e.target.value);
        setCity(e.target.value);
    }

    handleSubmit = (e) => {
        e.preventDefault();
        console.log("submitted");
        setFlag(false);
    }
    return(
        <div className='searchbar'>
            <form>
                <select defaultValue="default" onChange={handleChange}>
                    <option value="default" disabled>Choose a state...</option>
                    <option value="Alabama">Alabama</option>
                    <option value="Alaska">Alaska</option>
                    ...
                </select>
                <button type="button" onClick={handleSubmit}>Search</button>
            </form >
        </div>
    );
};

Advertisement

Answer

All the props are passed as the first argument of the component,

function Searchbar({setCity}, {setFlag}){

should be

function Searchbar({setCity, setFlag}){

Same apply for Footer

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement