Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass useState hook to child props in functional components

I'm trying to use the useState hook for deciding whether a modal opens or not.

I have this in the parent component.

    const handleState = (e) => {
        setModalOpen(e);
    };

    const [ modalOpen, setModalOpen ] = useState(true);

and I am currently passing the hook as follows

    return (
        <ListWrapper>
            {therapies.map((e, i) => {
                return (
                    <div ref={(e) => (ref.current[i] = e)}>
                        <CardVertical
                            title={e.title}
                            info={e.info}
                            img={e.img}
                            imgAlt={e.imgAlt}
                            functions={[ modalOpen, handleState ]}
                        />
                    </div>
                );
            })}
            <Modal
                img={therapies[0].img}
                imgAlt={therapies[0].imgAlt}
                title={therapies[0].title}
                info={therapies[0].info}
                functions={[ modalOpen, handleState ]}
            />
        </ListWrapper>
    );

In the modal I am getting and using the hook like this.

const Modal = ({ img, imgAlt, title, info, functions }) => {
    const [ open, setOpen ] = functions;
<Button
  onClick={() => {
    if (functions.modalOpen) {
        setOpen(false);
    }
  }}

I can read the open fine. But I cant seem to call the function.

My idea is that I'll change the information on the modal depending on what element is clicked in the array of elements. Probably by passing more state between the elements.

I'm thinking about maybe using context instead since somehow this feels like it's getting complicated.

like image 579
HichiHachi Avatar asked Mar 11 '20 12:03

HichiHachi


People also ask

Can we pass useState to child component?

You can pass the state as props, but if you want child components to alter state, then you can pass the setter from the useState directly to child components.

Can you pass useState function as props?

Passing useState in children component props to use itPassing useState as props in another component is totally possible. But there's no benefit in doing so because you can always call useState by importing React at the top of your JavaScript code and call it in all of your components.

Can we use useState in functional component?

Initialize useStateWe initialize our state by calling useState in our function component. useState accepts an initial state and returns two values: The current state. A function that updates the state.


3 Answers

you can pass the modalOpen and handleState separately like this

<Modal
    mg={therapies[0].img}
    imgAlt={therapies[0].imgAlt}
    title={therapies[0].title}
    info={therapies[0].info}
    isOpen={modalOpen}
    toggleModal={handleState}
 />

and use it in Modal component as

const Modal = ({ img, imgAlt, title, info, isOpen, toggleModal }) => {

<Button
  onClick={() => {
    if (isOpen) {
        toggleModal(false);
    }
}}
like image 173
Hemanath Avatar answered Oct 18 '22 01:10

Hemanath


you don't actually need to pass modalOpen and handleState separately. It should work anyway.

Plus, you dont need to define a new method to set the setter function. below is totally unnecessary.

const handleState = (e) => {
        setModalOpen(e);
    };

https://codesandbox.io/s/pass-usestate-to-child-c0qx6

like image 25
Barun Patro Avatar answered Oct 18 '22 02:10

Barun Patro


The issue you’re experiencing is that you’re passing an array as the value for the functions prop, but you’re trying to check functions.modalOpen as though it is a non-array object. This way you would need to check functions[0]. You might want to instead change what you're passing in like so:

functions={{ modalOpen, handleState }}

Then you can do functions.modalOpen as you have done.

This said, this is probably not the right way to go about this. modalOpen appears to be a boolean, not a function, and in any case, there's no need to wrap it in an object. Hemanath’s answer provides a good example of this..

like image 3
coreyward Avatar answered Oct 18 '22 03:10

coreyward