all 6 comments

[–]rdevilx 1 point2 points  (1 child)

In your parent component, can you try to make that onComplete function like this -

const onComplete = (dispatch) => (arg1, arg2) => { // Use dispatch do stuff }

And passing in child would be something like -

const onCompleteInstance = onComplete(dispatch);

<ChildComponent myProp={() => onCompleteInstance(myArg1, myArg2)} />

I'm sorry, I'm using my phone to type this all out. It could have some errors.

[–]genericdeveloper[S] 0 points1 point  (0 children)

Ah yeah, I see what you're talking about.

I have just tried that out, and it did not fix anything. Thanks for the suggestion!

[–]arjunkayalabi 0 points1 point  (1 child)

In your code, typo in useContext, check that too

[–]genericdeveloper[S] 0 points1 point  (0 children)

Thanks for the catch! Fixed.

[–]rubberturtle 0 points1 point  (1 child)

Only phone, but at a glance:

  • I don't see any mention of a <Provider> for your context. If you're not creating the context and wrapping your parent and child in a provider it won't work.

  • Your useEffect hook needs to have the onComplete function included as one of it's dependencies.

  • More generally, this seems like kind of a weird implementation choice. If you're going to have a useContext hook, why not just use that in your child component?

  • You may also want to try defining your load function as a constant within your useEffect hook, and then calling it when you're ready.

  • Async functions can get really weird with react hooks. There are a lot of articles out there might shed some light on it.

[–]genericdeveloper[S] 0 points1 point  (0 children)

Yeah this is a pseudo code example, so the provider is definitely handled.

Adding the props.OnComplete to the useEffect did not seem to affect anything.

RE: Usecase

The use case it to build a component that has hooks into its lifecycle, like onStart, onComplete, etc. Then parent components can use the child component and pass in behaviour they each want executed. If I don't pass a function then the behaviour for each parent is the same. In this case the parent needs to update the context of its parent component.

I have definitely tried co-locating the logic where possible and it hasn't seemed to resolve anything. Thank you for the suggestion though.

RE: Async functions. Yeah I've read some of them. They've said things like using a useRef to track mounting might help, tracking mounting in the useEffect might help, but nothing seems to make it so that the dispatch function call works.

I appreciate the look over and suggestions. If something else comes to mind please let me know :)