Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

326
Views
ReactJS: Save button value on submit

I have created a form, and I have two submit button. Now i'm trying to save the value from the button to make two different api call based precisely on the value of the button.

My problem is that sometimes the value from the button results "undefined" so when it is clicked nothing happend.

 return (
              <AvForm model={{}} onSubmit={saveEntity}>
    //.....
    <Button id="pdf" replace color="info" data-cy="entityCreatePDF" type="submit" onClick={buttonClicked} value="pdf">
                      <FontAwesomeIcon icon="save" />
                      &nbsp;
                      <span className="d-none d-md-inline"> PDF </span>
                    </Button>
                    &nbsp;
                    <Button color="primary" id="csv" data-cy="entityCreateCSV" type="submit" onClick={buttonClicked} value="csv">
                      <FontAwesomeIcon icon="save" />
                      &nbsp;<span className="d-none d-md-inline"> CSV </span>
                    </Button>
    
    )

  const buttonClicked = event => {
    console.log('event target', event.target.value);
    setButtonClick(event.target.value);
  };

  const saveEntity = (event, errors, values) => {
//....
 filteredEntities(params, sorting)
}


const filteredEntities = (params, sort) => {
    console.log('-- buttonClick --', buttonClick);
// there sometimes I receive undefined and so it doesn't enter in the if
    if (buttonClick === 'pdf') {
      getReportPDF(params, sort);
    }
    if (buttonClick === 'csv') {
      getReportCSV(params, sort);
    }
}

There is something that I should doing to avoid this behavior (i.e. that the key results in an undefined value and therefore does not allow me to take any action?).

Thank you

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Short answer:

Replace

  const buttonClicked = event => {
    console.log('event target', event.target.value);
    setButtonClick(event.target.value);
  };

with

  const buttonClicked = event => {
    console.log('event target', event.currentTarget.value);
    setButtonClick(event.currentTarget.value);
  };

Why? Sometimes you click the button itself, sometimes e.g. icon inside it - the element you click is actually a "target". "currentTarget" however allows the use of element the event listener was assigned to.

about 4 years ago · Juan Pablo Isaza Report

0

This issue might be arising from setState being asynchronous, ie, the state doesn't change immediately after setButtonClick is called. You can use a ref to sidestep this issue, if you don't need to re-render when buttonClick is changed, and use buttonClick.current to get the value.

const buttonClick = useRef();
...

const buttonClicked = event => {
    console.log('event target', event.target.value);
    buttonClick.current = event.target.value;
  };
...

const filteredEntities = (params, sort) => {
    console.log('-- buttonClick --', buttonClick.current);
    if (buttonClick.current === 'pdf') {
      getReportPDF(params, sort);
    }
    if (buttonClick.current === 'csv') {
      getReportCSV(params, sort);
    }
}

If you need to re-render when it changes, use both the state and a ref.

about 4 years ago · Juan Pablo Isaza Report

0

You can directly save the value to the state. As it is not an input field, no need to deal with event.target.value

.....
<Button onClick={() => setButtonClick('csv')}
<Button onClick={() => setButtonClick('pdf')}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!