react-navigation / rfcs

RFCs for changes to React Navigation
Other
88 stars 16 forks source link

Event emitter to communicate between header and screen component #20

Closed brentvatne closed 4 years ago

plus- commented 4 years ago

Is this pattern solved with v5.0? I'd like to use it but the emit function doesn't seem to be exposed in the useNavigation hook

satya164 commented 4 years ago

You can use setOptions to directly access screen's state and props in your header. An event emitter is not needed.

https://reactnavigation.org/docs/en/navigation-prop.html#setoptions---update-screen-options-from-the-component

plus- commented 4 years ago

Right,

What about function calls should I pass the function in the options or use a forward ref on the screen?

satya164 commented 4 years ago

Not sure what you mean. There's an example here https://reactnavigation.org/docs/en/headers.html#updating-options-with-setoptions

plus- commented 4 years ago

Say you have a screen with a form component, and the submit button is in the navigation header.

I don't want to pass the form content nor the submit function in the navigation params. My first though was to emit a submit event and listen it from the form, but it's not possible in v5 (no emit function available).

My current solution (not sure if it's the best) is to do a forward ref on the form component so the button can access the submit function (exposed with useImperativeHandle).

Is there a better pattern with v5 to handle this situation?

satya164 commented 4 years ago

Have you actually checked the links I shared?

plus- commented 4 years ago

Yes, but it was still not clear to me. This other link though is pretty straightforward: https://reactnavigation.org/docs/en/header-buttons.html#header-interaction-with-its-screen-component

cheers