Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackContext file error #7

Open
vaz007 opened this issue May 15, 2020 · 0 comments
Open

TrackContext file error #7

vaz007 opened this issue May 15, 2020 · 0 comments

Comments

@vaz007
Copy link

vaz007 commented May 15, 2020

I might be completely wrong or this error would be caused something from my end but I feel for some reason TrackContext file is not able to send functions through Context and Provider ?

I have attached TrackContext as well as createDataContext file.

TrackContext.js
`import createDataContext from "./createDataContext";
import trackerApi from "../api/trackerApi";

const trackReducer = (state, action) => {
switch (action.type) {
case "fetch_tracks":
return action.payload;
default:
return state;
}
};

const fetchTracks = (dispatch) => async () => {
const response = await trackerApi.get("/tracks");
dispatch({ type: "fetch_tracks", payload: response.data });
};
const createTrack = (dispatch) => async (name, locations) => {
console.log(name, locations);
await trackerApi.post("/tracks", { name, locations });
};

export const { Context, Provider } = createDataContext(
trackReducer,
{ fetchTracks, createTrack },
[]
);
`

createDataCOntext.js

`import React, { useReducer } from "react";

export default function createDataContext(reducer, actions, defaultValue) {
const context = React.createContext();

const Provider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, defaultValue);
const boundActions = {};
for (let key in actions) {
boundActions[key] = actionskey;
}

return (
  <context.Provider value={{ state, ...boundActions }}>
    {children}
  </context.Provider>
);

};

return { Context: context, Provider: Provider };
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant