valotas / preact-context

React new Context API for preact
Other
151 stars 8 forks source link

Is there any way to have access to this.context in component code? #30

Closed romansh closed 5 years ago

romansh commented 5 years ago

When we use react we are able to do something like App.contextType = OurContext; And then in, for example, componentDidMount() write let someVar = this.context.someVarFromContext to get last value of someVarFromContext from imported context.

How can I get the same - this.context.someVarFromContext using preact-context? Or is it not implemented?

valotas commented 5 years ago

Hi @romansh, do you have an example of what you are describing? I never heard of it before

romansh commented 5 years ago

Hi @valotas, there is an example in official docs of new react's context https://reactjs.org/docs/context.html#classcontexttype

We're using it in this way:

componentWillMount() {
        let wsUrl = window.wsUrl ? window.wsUrl : this.context.wsUrl;
        this.setState({wampPrefix: this.context.wampPrefix});
        this.setState({wsUrl: wsUrl});
    }

window.wsUrl is a variable from 'external' JS telling us an URL of our production websockets servers. If it's empty we use the test one from context. We can't use windows.variables inside render() because at time of building (with help of webpack/browserify etc) window object doesn't exists. So we have to work with it at another stage of lifecycle and use it in componentWillMount and then to use context to pass it in nested components.

Hope I have explained our reason to use this.context.

valotas commented 5 years ago

@romansh sorry for my late answer. Unfortunately this is not implemented. I can not think of an easy way implementing this without monkey patching preact's context.

valotas commented 5 years ago

@marvinhagemeister @developit any ideas on this? Do you plan to support this for X?

valotas commented 5 years ago

@romansh I'm afraid you have to wrap your component with a <Consumer /> for the moment :/

marvinhagemeister commented 5 years ago

We have added support for contextType in Preact X. Personally I'm not a fan of it because it ties the default value to the module resolution. As soon as a circular dependency emerges, The consumer will be called with different default values depending on where it's used. The same is true for React.

romansh commented 5 years ago

@marvinhagemeister, oh, that's great news, but where can I read about ways to use contextType in Preact X? We could not run the above react code using preact 8.4.2.

On https://preactjs.com/guide/differences-to-react I still can see "Contexts: Support for the legacy context API was added in Preact 3.0. Support for the new api is discussed as PR #963." There are not any news in PR #963

marvinhagemeister commented 5 years ago

@romansh Preact X is not yet released and lives in a separate private repo. It mainly started out as a random experiment and wasn't intended to be the successor to the v8 release line. We recently announced March 4th as the date where we'll merge the repo into the public one: https://www.youtube.com/watch?v=DFF9eOlTWzY

contextType will be used the same way as it is in react.

romansh commented 5 years ago

@marvinhagemeister, thank you for explanation. So we'll be waiting for the time when we can see the new Preact X. There is not much time left.

valotas commented 5 years ago

I do not think that it makes sense to implement contextType as part of preact-context then.