Search code examples
javascripthtmlreactjsfrontendmern

Getting only last element from this map method every time


OnSubmit I am calling handleSubmit function which has two parameters event, doc.

const handleRename = (e, doc) => {
    console.log(doc); 
}

now inside render section I have array and model form for each of that element like below:

{
   ArrayName.map((doc) => {
      <form onSubmit={(e) => handleRename(e, doc)}>
        ...
      </form>
}

instead of getting individual doc, I am getting last element of Array each time. Can anyone know what's going wrong ?

Thank you in advance


Solution

  • Try changing it to

    ArrayName.map((doc) => {
      return (<form onSubmit={(e) => {handleRename(e, doc)}}>
        ...
      </form>)
    

    Read this that page will explain best the error. Check the advanced syntax

    (e) => handleRename(e, doc) this section of your code, instead of returning a function it evaluates it immediately.