0

I have a list of items and want to iterate over them, create a div block with a button and if the user clicks on the button send this data to a function and then database. However with my code, instead of the item data, I only send undefined.

import React, {useState, useEffect} from 'react';
import './App.css';
import allItemsList from './allItemsList.json'

function App() {
  const itemsList = allItemsList[0].itemslist
  
  function addDemoButton (currentItem) {

    ///how do i pass the currentItem variable here to this function?

  }

  
  return (
    <div className="App">
      <div className="itembox">
      {itemsList.map((currentItem ) => (
                <div >
                  <h6>{`${currentItem}` }</h6>
                  <p>some information here</p>
                  <button onClick={(currentItem) => addDemoButton()}>Free Demo</button>
                </div>
            ))}
      </div>
    </div>
  );
}


this is the allitemslist.json that i import as allItemsList :


[
  {
    "itemslist": ["item1", "item2", "item3"]
  }
]


2 Answers 2

1

Just add it in addDemoButton as parameter of method

return (
    <div className="App">
      <div className="itembox">
      {itemsList.map((currentItem ) => (
                <div >
                  <h6>{`${currentItem}` }</h6>
                  <p>some information here</p>
                  <button onClick={() => addDemoButton(currentItem)}>Free Demo</button>
                </div>
            ))}
      </div>
    </div>
  );
Sign up to request clarification or add additional context in comments.

1 Comment

thats correct i hve missed the parameter. works perfectly. thanks.
1

In your code,

addDemoButton()

is empty without any parameter.

I believe you should put currentItem inside the function and called it.

addDemoButton(currentItem);

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.