U
U
uRoot2021-01-28 17:06:17
React
uRoot, 2021-01-28 17:06:17

How not to lose context in React when passing a function to props?

From the comp1 component I pass a bunch of parameters to the comp2 component, and from it to the comp3 component. Here I have a stupid component that I want to pass some of this data to.
But in comp3 I can't properly pass the function to the stupid component -- the this is lost.

comp3 looks like this:

function comp3({
    items,
    action,
    actionSceneNum
}) {

return(
    <Comp4
         handleRepeatActionClick: () => {
             actionSceneNum(false)
         },
    />
)}


As a result, actionSceneNum(false) does nothing. Inside this function, the state is set: Tell me how to fix it so that the state is set? The function with the content reaches comp3, the error is here:this.setState({ opened: isOpened })
handleRepeatActionClick: () => {
             actionSceneNum(false)
         },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alex4answ, 2021-01-28
@uroot

How not to lose the context:
1. bind the context myFunc.bind(this))
2. Close the contextmyFunct = () => {};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question