Calling onchange event function to useEffect

Viewed 30

I have a react application, where I have 2 radio buttons. on click on radio button, based on the clicked value I am calling an api. This is working fine, But I want my api to be called fist time when page loads with by default 1st radio button selected value. I am able to show 1st radio button selected by default and tried to call api on useEffect, but that is not working. Can anyone suggest possible solution for this. Below is my code:

//Radio button
<Radio.Group onChange={onAllVariableChange} value={contrVarOnly}>
          <Radio value={true}>Show contributing variables only</Radio>
          <Radio value={false}>Show all variables</Radio>
</Radio.Group>

//onchange function 
const onAllVariableChange= async (e) => {//Need to call this function so that api  get called on load also.
    let contrVar = false;
    if(e==null) contrVar = true;
    else contrVar = e.target.value;
    setContrVarOnly(contrVar);
    setIsModalLoading(true);
     contributingDataSets && contributingDataSets.map(study => {
        onContrVarOnlyChange(study, contrVar); //api call
     });
  }
0 Answers
Related