0

I am using a function in reactjs that will return a GrpahQL query result. My code is following

...
const fetchData = async (url, query, param, variable) => {
        return await axios.post(url, {
            query: query,
            variables: { param: variable }
        })
    };
...

I want to change the param of variables dynamically from the function value.

How can I pass the param to variables?

2
  • 1
    you mean like so { [param] : variable } ... computed property ? Commented Sep 20, 2022 at 6:56
  • 1
    Works like a charm. please post it as an answer. I will tick mark on it. Commented Sep 20, 2022 at 7:13

1 Answer 1

2

you are looking for

computed property names in Object MDN DOCS

so it would be something as

{ [param] : variable }

so now param refers to some variable value instead of "param" as a string 😉

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

Comments

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.