0

I'm trying to use redux-thunk to getData asynchronously using hooks in redux. I can get it all working with connect() but useSelector/useDispatch is where I can't figure it out. codesandbox

import React, { useEffect, useState } from "react";
import { shallowEqual, useSelector, useDispatch } from "react-redux";
import { getData } from '../Actions/index'

const MissionForm = props => {
  const [send, setSend] = useState(false)
  const dispatch = useDispatch()
  const data = useSelector(
    state => ({
      // missions: state.missions,
      isFetchingData: state.isFetchingData
    }),
    shallowEqual
  );


  useEffect(() => {
    if(send) {
      console.log('im', send)
      dispatch(getData())
    }
  }, [])

  const handleGetData = e => {
    setSend(true)
    e.preventDefault();
  }

  return (
    <>
      {data.isFetchingData ? (
        <div>We are fetching data...</div>
      ) : (
        <button onClick={() => dispatch(getData)}>Get Mission</button>
      )}
    </>
  );
};

export default MissionForm;

This is the Action from '../Actions/index'

export const getData = () => dispatch => {
  console.log('step 1 sent')
  dispatch({ GET_DATA });
  console.log('thunky worked')
  axios('https://api.spacexdata.com/v3/missions')
    .then(response => {
      console.log(response)
      dispatch({
        type: UPDATE_MISSIONS,
        missions: response
      })
    })
    .catch(error => console.log(`Couldn't retrieve data.`, error))
};
Advent
  • 35
  • 1
  • 6
  • What is the current and expected behavior? Are there errors? – Brian Thompson Mar 19 '20 at 21:08
  • The code looks valid as-is. What actual problem are you seeing? – markerikson Mar 19 '20 at 21:11
  • Your effect has missing deps: `dispatch` and `send`, you should use [exhaustive deps](https://github.com/facebook/react/issues/14920) linter. Is the action dispatching, the fetch working and data set in the store? If all data is set in the store then the selector may be your problem. – HMR Mar 19 '20 at 21:12
  • here's the codesandbox. the data doesn't populate.something isnt workking with it. https://codesandbox.io/s/blue-bash-0h726 – Advent Mar 19 '20 at 21:40

0 Answers0