Z
Z
Zhanna_K2020-10-11 21:34:25
React
Zhanna_K, 2020-10-11 21:34:25

Request to get data in useEffect(). What is the problem?

I'm trying to load an array of tasks from the database into the component via api. The code is as follows:
This is how I use the effect hook

const [tasks, setTasks] = useState([])
const [isLoading, setIsLoading] = useState(false)

 useEffect(() => {
    const fetchData = async (id, fetchFunc) => {
      setIsLoading(true);
      const tasks = await fetchFunc(id);
      setTasks('tasks from fetch', tasks);
      setIsLoading(false);
    };
    fetchData(projectId, fetchTasks);
  }, []);


Actually the actions themselves:
export const  recieveTasks = (payload) => {
  return {
    type: GET_PROJECT_TASKS,
    tasks: payload,
  };
}

export const fetchTasks = (id) => (dispatch) => {
  API.getProjectTasks(id).then((tasks) => dispatch(recieveTasks(tasks)));
};

What I see in the logger:
5f834ee666400075791206.png
the data came from the server, the action is dispatched. Why tasks = undefined? After all, I use async await and we are waiting for data to be received from fetch.

Tell me what I don't get?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question