1

How to call Functions with parameter dynamically in ReactJS when selected a field

While selecting the element from the select drop-down, I stored that value in the state variables

<Select
onChange={e => setMessage(e.value)}
options={options}
>

After that, I need to call another function which this parameter and I did this, but it doesn't take any argument

<CallGetAMIDetails />

What will be the right syntax to execute the function with parameter

function CallGetAMIDetails(message)

This is wrong syntax <CallGetAMIDetails(message) />

Here is the full example code, what would be right thing to do to call this function function CallGetAMIDetails(message)

import React , { useState }from 'react';
import {DashboardLayout} from '../components/Layout';
import Select from 'react-select'

const options = [
    { value: 'ami-abc*', label: 'ami-abc' },
    { value: 'ami-xyz*', label: 'ami-xyz' },
]

const DiscoverAMIPage = () => {
    return (
        <DashboardLayout>
            <h2>Discovered AMI</h2>
            <Select
            onChange={e => setMessage(e.value)}
            options={options}
             />
        <CallGetAMIDetails />
        </DashboardLayout>
    )
}

  function CallGetAMIDetails(message) {
    console.log(this.message)
 }

export default DiscoverAMIPage;

2 Answers 2

1

CallGetAMIDetails isn't a component, it's just a normal function. You'd call it like any other function. (Like how you call setMessage or console.log functions already.) For example:

CallGetAMIDetails(someValue)

Are you trying to call it in the onChange handler? If so, it would be like this:

onChange={e => {
  setMessage(e.value);
  CallGetAMIDetails(someValue);
}}

Basically you'd just wrap the change handler's function in curly braces {} so you could add another line of code to it.

Note of course that someValue is just a placeholder here, since I don't know what you want to actually pass to it. If you're manually creating the function argument then it could be something like:

CallGetAMIDetails({ message: 'some message' });
Sign up to request clarification or add additional context in comments.

Comments

1

You can try to pass your message state under CallGetAMIDetails components like this

<CallGetAMIDetails callMessage={message} />

And after use it in the component as props :

    CallGetAMIDetails = ({callMessage}) => {
       // Do somethings
    }

Every time your state is update with setMessage() useState function, your message state is updated and so your CallGetAMIDetails component too

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.