Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion]: Add a new page for how to communicate between components. #6676

Closed
woochanleee opened this issue Mar 4, 2024 · 4 comments
Closed

Comments

@woochanleee
Copy link

woochanleee commented Mar 4, 2024

Summary

Add a new page for how to communicate between components.

Page

No response

Details

I think it would be helpful to have a page that explains how to communicate between components. This could include a best practice example of a communication between two components that don't have a parent-child relationship.

In the past, old React docs already had these content.
https://web.archive.org/web/20151124041030/http://facebook.github.io:80/react/tips/communicate-between-components.html

References

https://stackoverflow.com/questions/21285923/how-can-i-communicate-between-related-react-components#comment45951998_21285923

@rickhanlonii
Copy link
Member

This is documented on these pages, depending on what you're looking to achieve:

The .bind specific technique is briefly mentioned in the Legacy Component docs here, but createReactClass is deprecated and will not be added to the new site. See the legacy site for createReactClass docs.

@rickhanlonii rickhanlonii closed this as not planned Won't fix, can't repro, duplicate, stale Mar 5, 2024
@woochanleee
Copy link
Author

woochanleee commented Mar 5, 2024

Nowhere in the documentation does it say.

For communication between two components that don't have a parent-child relationship, you can set up your own global event system. (Old React Docs said)

Is using a global event system not a good practice and only using context(or lifting state up) is best practice?

@rickhanlonii
Copy link
Member

Correct, the only way to do it safely with concurrent features today is to de-opt with useSyncExternalStore, which has limitation such as forcing fallbacks for existing content. We're aware this is a pain point, though we don't have any solutions to recommend until we build better support for it.

@woochanleee
Copy link
Author

woochanleee commented Mar 5, 2024

Thank you. I found an example that uses useSyncExternal, I'll check it out.

facebook/react#14988 (comment)
https://codesandbox.io/p/sandbox/hopeful-sara-fb970f?file=%2Fsrc%2Findex.js%3A1%2C1-47%2C1

I would be happy if React provided a built-in event emitter system like Angular, Vue. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants