I'm trying to get data API from star wars website with react project and here's my code:
const Form = (props) => {
const [search, setSearch] = useState("people");
const [searchID, setSearchID] = useState(1);
const [responseData, setResponseData] = useState({});
useEffect(() => {
buttonAPI();
setSearch(props.type);
}, [props.type]);
const buttonAPI = () => {
axios
.get(`https://swapi.dev/api/${props.type}/`)
.then((res) => {
setResponseData(res.data);
})
.catch((err) => {
console.log(err);
});
};
const onSubmit = (e) => {
e.preventDefault();
navigate(`/${search}/${searchID}`);
};
return (
<div className="container" style={pageStyle}>
<form onSubmit={onSubmit}>
.
.
.
)
I'm getting this error while trying to add props.setSearch(props.type); into useEffect
Here is my github for this project:
https://github.com/nathannewyen/reactjs-practice/tree/master/luke-api
setSearchis not passed as a prop to the component, in fact is available to the component due to theuseState()hook. Just callsetSearch(props.type)and it should work. Also remember to pass an array in order to run the effect only when a relevant prop or state property is updated: reactjs.org/docs/…props.typeis undefined in my axiosFormcomponent and make sure is a function. Something like this:<Form type={someFunction} />Just saw below that the comment by @yash addresses the same thing :D