Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dispatch argument to returned function

I am dispatching a function like the following:

ChartHandlePureFunc.js:

export function getBasic() {

  return async (dispatch) => {
    const response = await axios.get
      `https://financialmodelingprep.com/api/v3/historical-price-full/AAPL?apikey=*********`;
      dispatch(changeValue(response.data));
  };
}

ChartHandler.js:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

useEffect(() => {
   if (!req) {
     dispatch(getBasic());
   }

}, [req]);

The above code works and the only thing I am trying to change is for getBasic to be able to take an argument and be dispatched like the following:

export function getBasic(userInput) {

  return async (dispatch) => {
    const response = await axios.get(`https://financialmodelingprep.com/api/v3/historical-price-full/${userInput}?apikey=*********`);
    dispatch(changeValue(response.data));
  };
}

And then in my other components useEffect:

useEffect(() => {
        if (!req) {
          dispatch(getBasic('AAPL'));
        }
    
      }, [req]);

Since the argument is only getting used in the getBasic function I believe this is all the code required for the problem, I have also tried having userInput next to dispatch like:

return async (dispatch, userInput) => {
    ...
};

And have tried that with both userInput still being in the getBasic argument and without.

Edit:

console.log(userInput) when userInput is in the getBasis arg like: getBasic(userInput) I get AAPL, the correct result in console, but if I move it down to return async (dispatch, userInput) I then get

ƒ i() {
        var e,
            r = (e = 
             t.getState()).computedStates[e.currentStateIndex].state;
        return void 0 !== r && (n = r), n;
      }

in console

>Solution :

You can take the param and make a template string like this:

export function getBasic(userInput) {
const requestUrl = `https://financialmodelingprep.com/api/v3/historical-price-full/${userInput}?apikey=*********`;

  return async (dispatch) => {
    const response = await axios.get(requestUrl);
    dispatch(changeValue(response.data));
  };
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading