0
const Test = ({ inventory }) =>
<div className="row">
    {inventory.map((item, i) =>
        <div className="item" key={"item_" + i}>
            <div className="card hoverable col s3">
                <img onClick={buyItem(i)} src={item.img} alt="" />
                <div className="container center-align">
                    <h4 className="">
                        {item.name}
                    </h4>
                    <p>
                        Price: {item.price}
                    </p>
                    <p>
                        Average price paid: {item.averageDisplay}
                    </p>
                    <p>
                        Amount owned: {item.owned}
                    </p>
                    <button
                        className="btn"
                        data={item.index}
                        onClick={sellItem(i)}
                        name="button"
                    >
                        Sell
                    </button>
                </div>
            </div>
        </div>
    )}
</div>;

ReactDOM.render(<Test inventory{inventory}/>, 
document.getElementById("container")
);

inventory is an array of objects, I've successfully been able to loop through in this example, but for some reason I can't figure out how to pass the value of i into a function despite that it's already being used to generate unique keys.

2 Answers 2

1

You need to create a new function

onClick={() => sellItem(i)}
Sign up to request clarification or add additional context in comments.

Comments

0

Use it like this by creating a function call

onClick={(event) => buyItem(event, i)}

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.