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

feat(server): useProps context #1385

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

Conversation

deer
Copy link
Contributor

@deer deer commented Jul 1, 2023

closes #755

This change allows things like this:

export default function Grandchild() {
  const props = useProps();
  if (props.data === "computing this is really hard") {
    return <div>{props.url.toString()}</div>;
  }
  return <div></div>;
}

So components can get access to the props from the root component by using the newly introduced useProps context.

This is a generalization of the PR that was originally opened to close this issue. I thought it might be convenient to have not just the route available, but the entire props. I'm opening this as a draft since I didn't write any documentation yet, and I'm concerned about performance implications.

@marvinhagemeister, would you be so kind to clarify what sort of performance problems this will introduce? I see https://stackoverflow.com/questions/75060633/react-context-performance-and-suggestions and they talk about when context changes, the components re-render. Since we're just rendering this all on the server and the context is only created once and never changes, maybe it doesn't matter.

Mildly related to #1339.

@marvinhagemeister
Copy link
Collaborator

@deer Performance of context providers is not a concern on the server. It's mostly just a property access which costs nothing. It matters much more on the client where things are stateful and context updates can cause an app to re-render.

@deer deer marked this pull request as ready for review July 2, 2023 06:38
@deer
Copy link
Contributor Author

deer commented Jul 2, 2023

Thanks for the clarification Marvin. I've tried my hand at writing some documentation for this and have therefore marked it as ready.

@deer deer marked this pull request as draft July 10, 2023 07:37
@deer deer marked this pull request as ready for review July 10, 2023 07:49
@deer
Copy link
Contributor Author

deer commented Jul 10, 2023

Another FYI: we'll need to update the ToC to include this new example when this feature gets released in the next version. Currently the doc is just in the canary ToC. (Assuming this gets approved!)

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