I keep getting this reference error and no idea what is wrong.
ReferenceError: Cannot access 'search' before initialization App C:/Users/GS66/Desktop/IN20/IFN666/week4/src/App.js:60 57 | 58 | export default function App() { 59 |
60 | const { loading, headlines, error } = useNewsArticles(search); 61 | const [search, setSearch] = useState(""); 62 | 63 | if (loading) {
This is the App part in App.js
export default function App() {
const {loading, headlines, error} = useNewsArticles(search);
const [search, setSearch] = useState("");
if (loading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Something went wrong: {error.message}</p>
}
return (
<div className="App">
<h1>News Headlines</h1>
<SearchBar onSubmit={setSearch}/> {
headlines.map((headline) => (
//headline is now an object
<Headline key={headline.url} title={headline.title}/>
))
}
</div>
);
}
And this is the useNewsArticles function part from api.js.
export function useNewsArticles(search) {
const [loading, setLoading] = useState(true);
const [headlines, setHeadlines] = useState([]);
const [error, setError] = useState(null);
useEffect(() => {
(async() => {
try {
setHeadlines(await getHeadlines(search));
setLoading(false);
} catch (err) {
setError(error);
setLoading(false);
}
})();
}, [search]);
return {
loading,
headlines,
error,
};
}
searchbefore you define it, which happens one line further down... The:60in the error means line 60 so this way you know which line the error is talking about