I am working on some code, and I am stuck on this error. I have not seen it come up before in my other projects, and so I am not sure why it is here. I am using the useEffect hook, and I want to update it when a user chooses an option from a drop down. What happens is that when a user chooses an option, i get the following error TypeError: Assignment to constant variable.
. I understand that it is saying that I am trying to update a constant which cant happen, but I dont get why.
Unfortunalty,
I can not determine how to properly render my code, but the next two lines are the lines of code that I am having the issue with. These are then followed by the full code that I am working on.
const [TitleId, setTitleId] = useState(0);
const TitleSelect = event =>{
console.log(event.target.value);
setTitleId=(32);
};
import React, {useState,useEffect} from 'react';
// import '../app.css';
const Header = props=> {
const [TitleList, setTitleList] = useState([]);
const [TitleId, setTitleId] = useState(0);
useEffect(()=>{
console.log('first start');
setTitleList(
props.data.map((title,index)=>({
title: title.title,
id: index
}))
)
},[])
const TitleSelect = event =>{
console.log(event.target.value);
setTitleId=(32);
};
let content = (
<select
onChange={TitleSelect}>
{TitleList.map(title=>(
<option key={title.id} value={title.id}>
{title.title}
</option>
))}
</select>)
return(content);
}
export default Header;
Any suggestions or advice is greatly appreciated.
setTitleId=(32);
is causing error you meant setTitleId(32);
instead.
By doing setTitleId=(32)
you were trying to assign a value to a const declaration that holds reference to a function pointer returned by useState
thus you got Assignment to const variable
error