0

Good morning,

I don’t know if my title is understandable but I would like to be able to have a const with parameters to be able to choose what to send with axios. Basically i have this :

const {send1='', send1_1= '', send2='', send2_2 = '' } = this.state;

await axios.post(' MY_API/default/serverlessAppFunction', 
{ key1: `${send1}, ${send1_1}`, key2: `${send2}, ${send2_2}`},);

I would like to know if it was possible to declare a variable somewhere else, to put it in axios. A little bit like this :

const key3 : `${test3}, ${test3_3}`;

const {send1='', send1_1= '', send2='', send2_2 = '', test3= '', test3_3=''} = this.state;

await axios.post(' MY_API/default/serverlessAppFunction', 
{ key1: `${send1}, ${send1_1}`, key2: `${send2}, ${send2_2}`, key3},);

The example it's not working of course.

But globally, i would like to do a thing like that. And if it's possible, How do I declare this variable and in what form?

i'm talking about declare like this or in another way

const key3 = "${test3}, ${test3_3}";

(Sorry if I’m not specific, I don’t speak very well English) Thanks.

7
  • Yes, it is possible. Do you face any issue?? Mention that issue in your question? Commented Nov 25, 2019 at 9:08
  • If you want your state to be available globally you can use redux for the state management, is there any issue with the example you provided in the question? Commented Nov 25, 2019 at 9:10
  • @PardeepSharma i have updated my post with a question. I don't know if it's really understandable :/ sorry if it's not Commented Nov 25, 2019 at 9:12
  • @YogeshDevgun i want to know how can i declare the key3 Commented Nov 25, 2019 at 9:13
  • 1
    @YogeshDevgun it was that. Thank you. But I don't know how to put your comment in "green" sry. Commented Nov 25, 2019 at 9:27

1 Answer 1

1

You can declare like this as constants and use anywhere:

const key1 = ${send1}, ${send1_1} ;

const key2 = ${send2}, ${send2_2} ;

const key3= ${send3}, ${send2_3} ;

await axios.post(' MY_API/default/serverlessAppFunction', { key1, key2, key3 });

It is shorthand for:

await axios.post(' MY_API/default/serverlessAppFunction', { key1: key1, key2: key2, key3: key3 });

Hope that answers your question.

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.