Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lifting State with hooks (from a mapped array)

I'm confused about how I'm supposed to raise events from a child component using hooks (or stateless components rather). Maybe I'm thinking too much. Or not enough! I have built a simple example illustrating my confusion.

Say we have a parent component with some data

import React, { useState } from "react";
import ReactDOM from "react-dom";

const Parent = () => {
  const data = [
    {
      thing: 1,
      info: "this is thing 1"
    },
    {
      thing: 2,
      info: "this is thing 1"
    },
    {
      thing: 3,
      info: "this is thing 3"
    }
  ];

  function handleClick(item) {
    console.log(item);
  }

  return (
    <div> 
    <h1> This is the Parent </h1> 
    <Child data={data} onShowClick={handleClick} /> 
    </div>
  )
};

And child components created from mapping through the data

const Child = (data, {onShowClick}) => {
  return (
    <ul> 
      { data.data.map(item => {return (
        <li key={item.thing}>{item.info}
        <button onClick={() => onShowClick}>Click</button>  
        </li>
      )})}
    </ul> 
  )
}

If this was all found in the same component I would do something like

onClick={() => handleClick(item)}

But you can't pass an argument with a prop.

onClick={(item) => onShowClick}
// Or
onClick={onShowClick(item)}

Maybe hooks are confusing me. Any direction would be appreciated.

like image 935
Jacob Avatar asked May 24 '26 02:05

Jacob


2 Answers

It's easy buddy. check below code. I just made some changes in you code.

const Parent = () => {
    const data = [
        {
            thing: 1,
            info: "this is thing 1"
        },
        {
            thing: 2,
            info: "this is thing 2"
        },
        {
            thing: 3,
            info: "this is thing 3"
        }
    ];

    const handleClick = (item) => {
        console.log(item);
    }

    return (
        <div>
            <h1> This is the Parent </h1>
            <Child data={data} onShowClick={handleClick} />
        </div>
    )
};

const Child = (props) => {
    return (
        <ul>
            {props.data.map(item => {
                return (
                    <li key={item.thing}>{item.info}
                        <button onClick={() => props.onShowClick(item)}>Click</button>
                    </li>
                )
            })}
        </ul>
    )
}

Hope this is helpful.

like image 130
Nishith Patel Avatar answered May 25 '26 16:05

Nishith Patel


This has nothing to do with hooks.

You should check the documentation on how to pass arguments to event handlers: https://reactjs.org/docs/handling-events.html#passing-arguments-to-event-handlers

This is the example from the documentation.

<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>

But since you do not need e, you just going to pass item

{
  data.data.map(item => (
    <li key={item.thing}>{item.info}
      <button onClick={() => onShowClick(item)}>Click</button>
    </li>
  ))
}
like image 23
Ertan Hasani Avatar answered May 25 '26 17:05

Ertan Hasani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!