0

I am receiving the error vesselList is not a function in my homescreen that's using a redux slice the error is in vesselList() in the dispatch in the useEffect hook please suggest if there is a better way i can reform this code.

the Slice : vesselSlice :

export const vesselSlice = createSlice({
  name: "vesselList",
  initialState: {
    vessels: [],
  },
  reducers: {
    vesselList: (state, action) => {
      state.value = action.payload;
    },
  },
});

export const {
  vesselList,
} =
  (keyword = "") =>
  async (dispatch) => {
    try {
      dispatch({ type: VESSEL_LIST_REQUEST });
      const { data } = await axios.get(
        "http://127.0.0.1:8000/api/vessels/info"
      );

      dispatch({
        type: VESSEL_LIST_SUCCESS,
        payload: data,
      });
    } catch (error) {
      dispatch({
        type: VESSEL_LIST_FAIL,
        payload:
          error.response && error.response.data.detail
            ? error.response.data.detail
            : error.message,
      });
    }
  };
export default vesselSlice.reducer;

HomeScreen.js :

function HomeScreen() {
  const dispatch = useDispatch();
  const Listvessel = useSelector((state) => state.vesselList);
  const { error, loading, vessels } = Listvessel;
  useEffect(() => {
    dispatch(vesselList());
  }, [dispatch]);
  return (
    <div>
      Fleet vessels :
      <div className="fleet-vessels-info">
        {vessels.map((vessel) => (
          <VesselCard vessel={vessel} />
        ))}
      </div>
    </div>
  );
}

export default HomeScreen;

1 Answer 1

1

Because vesselList not yet export, why you not use createAsyncThunk to fecth url and dispatch to redux store?

Sign up to request clarification or add additional context in comments.

2 Comments

but i have export const { vesselList, } in my slice is that not the same ?
In docs, export action like: export const { vesselList, } = vesselSlice.action

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.