-
Notifications
You must be signed in to change notification settings - Fork 66
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
add 'contextConsumer' #165
Conversation
It currently works on the client side, but not in SSR: let ctx = createContext("de")
let inline consume() = contextConsumer ctx (fun lang -> str lang)
// ...
let view model =
// ...
str "This should be the default value (de):"
consume()
contextProvider ctx model.Language [
str (sprintf "This should be the model value (%s):" model.Language)
consume()
contextProvider ctx "xyz" [
str (sprintf "This should be the overridden value (%s):" "xyz")
consume()
]
str (sprintf "This should be the model value (%s):" model.Language)
consume()
]
str "This should be the default value (de):"
consume()
Clientside: SSR: I think it renders the elements too early, so that my stack pushing/popping is completely useless ... Since SSR is currently incorrect, anyway, this is not a regression, so i think I'll revert that part and just PR the new 'contextConsumer' function. What do you think? |
This is due to my "cheap solution" in order to support the context API in .NET without many code changes. As it's currently difficult to pass a context object through the nested rendered elements in SSR, I'm just using the default value and hoping the proper one will be used on the client side when mounting React. Does it happen like this in your example? Maybe it'd be better to fix it in SSR (your note in the comment is already helpful!) but I'm not sure what would be the easiest way to do this. Any ideas? In any case, thanks for the PR! |
/// More info at https://reactjs.org/docs/context.html#contextconsumer | ||
let inline contextConsumer (ctx: IContext<'T>) (children: 'T -> ReactElement): ReactElement = | ||
#if FABLE_COMPILER | ||
ReactBindings.React.createElement(ctx?Consumer, createObj [], [!!children]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can pass null
here instead of createObj []
.
Yes, after the first client-side render all is ok. In my use-case (localization) it doesn't matter anyway, there is only one context anyway.
No simple idea, the rendering would need to be delayed so that the parent can influence the child. |
Ok, if the client side fixes this let's consider it good enough for now ;) Released as 5.2.4, thank you! |
closes #164
Note: this is currently completely untested.