2

I have a service that returns an array of records in 'res'. Works great in my REACT components. I need to retrieve the same data in a function but I think my syntax is wrong as I get no data.

Here is the code for the service: ...

import axios from 'axios';
import * as myConstants from '../Services/User_API_Base_URL'
class UserService {
    getUsers(){
        return axios.get(myConstants.USER_API_BASE_URL + "users");
    }
}
export default new UserService() 

...

Here is the code in a Component that works great: ...

componentDidMount= async (event)=>{
  UserService.getUsers().then((res) => {
        this.setState({ users: res.data});
    });  
  }

...

Here is the code in the Function that does not appear to get anything into 'datau' ...

const [datau, setDatau] = useState([]);
  useEffect(() => {
    const doFetchuser = async () => {
      setDatau(UserService.getUsers())
    };
    doFetchuser();
  }, []);

...

Any help getting this to work would be greatly appreciated. I am just starting to get familiar with Functions\Hooks, etc.

Rob

HighwayRob
  • 119
  • 2
  • 11

1 Answers1

0

Thank you to @jsNOOb:

Here is the working code

const [data, setData] = useState([]);
  useEffect(() => {
    const doFetch = async () => { const res = await UserService.getUsers(); 
      setData(res.data) };
    doFetch();
  }, []);
HighwayRob
  • 119
  • 2
  • 11