score:4
Accepted answer
import React, { useState, useEffect } from 'react';
import Axios from "axios";
import {NavLink, useParams} from "react-router-dom";
import Spinner from '../../Spinner';
function MentorList() {
const [categoriesResult, setCategoriesResult] = useState([]);
const [mentorsResult, setMentorsResult] = useState([]);
const [categoriesToId, setCategoriesToId] = useState({});
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
const { categoryId } = useParams();
useEffect(() => {
const fetchCategories = () => {
Axios.get("https://localhost:4000/categories", {}).then((response) => {
if (response.data.status === "ok") {
setCategoriesResult(Object.keys(response.data.result));
setCategoriesToId(response.data.result);
setLoading(false);
} else {
setError(response.data.error);
setLoading(false);
}
}).catch(err => {
console.dir(err);
setError("There was an error signing you in. Please try again later.");
setLoading(false);
});
};
fetchCategories();
}, []);
useEffect(() => {
const fetchMentors = () => {
console.dir('test');
Axios.get("https://localhost:4000/view-by-category/" + categoryId, {}).then((response) => {
if (response.data.status === "ok") {
setMentorsResult(Object.keys(response.data.result));
setLoading(false);
} else {
setError(response.data.error);
setLoading(false);
}
}).catch(err => {
console.dir(err);
setError("There was an error with the request. Please try again later.");
setLoading(false);
});
};
if (categoryId) {
fetchMentors();
}
}, [categoryId]);
if (loading) {
return <Spinner />;
}
if (mentorsResult && categoriesResult) {
var mentors = mentorsResult.map(mentor => {
return <div className="row">
<p className="mentor-profile-name">{mentor.name}</p>
<p className="mentor-profile-bio">{mentor.shortBio}</p>
<NavLink to={`/mentor/${mentor.id}`} exact>View Profile</NavLink>
</div>
})
var categories = categoriesResult.forEach(item => {
return <div className="row"><NavLink to={`/mentors/${item.id}`} exact>{item.name}</NavLink></div>
});
return <div className="container">
<div className="col-3">
{categories}
</div>
<div className="col-9">
{mentors}
</div>
</div>
} else {
return <div></div>;
}
};
export default MentorList;
Source: stackoverflow.com
Related Query
- React Hook is called in function that is neither a React function component nor a custom React Hook function
- React Hook "useParams" is called in function "fetchMentors" that is neither a React function component nor a custom React Hook function
- React Hook "useTranslation" is called in function "getMyMenu" that is neither a React function component nor a custom React Hook function
- React Hook "useForecast" is called in function "getSearch" that is neither a React function component nor a custom React Hook function
- React Hook "useState" is called in function that is neither a React function | Typescript
- React Hook "useAxios" is called in function that is neither a React function component nor a custom React Hook function
- React Hook "React.useState" is called in function "form" that is neither a React function component nor a custom React Hook function
- React Hook "useSelector" is called in function "posts" that is neither a React function component nor a custom React Hook function
- React Hook "useAxios" is called in function "onFinish" that is neither a React function component nor a custom React Hook function
- React Hook "useState" is called in function "app" which is neither a React function component or a custom React Hook function
- React Hook "useEffect" is called in function "shoes" which is neither a React function component or a custom React Hook
- React Hook "useContext" is called in function "age" which is neither a React function component or a custom React Hook function
- React Hook "useState" is called in function "setResults" which is neither a React function component or a custom React Hook function
- React Hook "useState" is called in function "test" which is neither a React function component or a custom React Hook function
- React Hook "useStyles" is called in function which is neither a React function component or a custom React Hook function
- React Hook is called in function "onSubmit" which is neither a React function component or a custom React Hook function
- Failed to compile React Hook "useQuery" is called in function "home" which is neither a React function component or a custom React Hook function
- React Hook is called in function which is neither a React function component or a custom React Hook function
- @apollo/client error-React Hook "useQuery" is called in function which is neither a React function component or a custom React Hook function
- React Hook "useDispatch" is called in function "setApprovedDecision" which is neither a React function component
- useNavigate is giving an error -- React Hook "useNavigate that is neither a React function component nor a custom React Hook function
- React Hook "useState" is called in function "AAA" which is neither a React function component or a custom React Hook function
- React Hook "useEffect" is called in function "handleNameChange" which is neither a React function component or a custom React Hook function
- React Hook "useSelector" is called in function
- React + Antd + Rollup Component Library "Error: Invalid hook call. Hooks can only be called inside of the body of a function component"
- React 17.0.1: Invalid hook call. Hooks can only be called inside of the body of a function component
- React Hook "useState" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function
- when is the useEffect hook clean up function get called in react
- React Hook "useDispatch" cannot be called at the top level. React Hooks must be called in a React function component or a custom React Hook function
- React Hooks must be called in a React function component or a custom React Hook function
More Query from same tag
- Unable to run a function inside another function: Cannot read property 'test' of undefined / Cannot read property 'setState' of undefined
- How to get a config value into a href as string instead of object object
- semantic ui react default selected options in dropdown
- Storybook 6 - how to set array of objects?
- Firestore how to extract sub collection from collections
- Update navbar after success login or logout redirection
- Use a linear gradient in a background image
- Identifier is expected || Unexpected token
- Is there a cleaner way to stop React rendering on initial load?
- Babel 6 + React - Passing initial data (In-browser transpiler)
- Should we use useCallback in every function handler in React Functional Components
- How do you deal with public and private routes in a NextJS app?
- Introducing errors when refactoring within <Route /> in React front end
- What type can i set to the first param of setState in typescript? how can I pass dynamiclly state object to setState?
- Accessing data inside json object error
- How to pass id to React Js path link?
- How to feed two children to React Component and use values?
- Redirect from within a React.Component
- warning : is defined but never used
- Cannot get props from mapStateToProps
- Root div padding React (Styled Components)
- Getting Gitlab API data in a simple node/react app
- How to pass state to Link from reach-router
- code splitting with Loadable from react-loadable causes screen flicker
- How to add input value as a tag to an object array and display it in react js
- Building a button only with image in React
- How to in React Component constructor set this state
- Material UI AutoComplete React. Display different options in list and save different option in form state (with multiple values)
- Passing props to useState React
- shouldComponentUpdate in function components