Comments (10)
I'll probably take a shot at this too, since I was just experimenting with the new Context API the other day.
from react-media-query-hoc.
Cool - just needs to be backwards compatible with react 15 for the time being (as ideally want this lib to still work with 15 for now)
from react-media-query-hoc.
@jooj123 Ok. I can either create a separate media-query-context-provider component and only export the Provider and Consumer if React.createContext exists, so it is only supplied in React 16+, or I could use create-react-contex that polyfills the functionality for React 14 + 15. Do you have a preference?
from react-media-query-hoc.
https://github.com/jamiebuilds/create-react-context looks good 👍
Not sure yet if the move to createContext
will change the usage of react-media-query-hoc
.. hopefully not.
Before you spend time on this maybe just confirm if there will be breaking changes to the interface?
from react-media-query-hoc.
I'm pretty sure I can do it without modifying the Provider API. That would still take children and wrap them in its context. We could also expose MediaQuery.Provider and MediaQuery.Consumer components so users could have all of the flexibility that the new Context API provides.
e.g.:
import {MediaQueryContext as MediaQuery} from 'react-media-query-hoc';
const mediaQueries = const mediaQueries = {
oneColumn: "screen and (max-width: 991px)",
twoColumn: "screen and (min-width: 992px)",
};
function ResponsiveComponent(props) {
return props.media.oneColumn ? (
<div>Only one column!</div>
) : (
<div>First column!</div>
<div>Second column!</div>
);
}
function MediaQueryWrapper(props) {
return (
<MediaQuery.Provider queries={mediaQueries}>
<div id="otherDocumentStructure">
<div id="yetMoreStructure">
<MediaQuery.Consumer>
{media => (
<ResponsiveComponent media={media} />
)}
</MediaQuery.Consumer>
</div>
</div>
</MediaQuery.Provider>
);
}
from react-media-query-hoc.
Keeping the existing api to avoid breaking changes is good 👍
Can you provide an example where exposing MediaQuery.Provider
and MediaQuery.Consumer
might be useful?
The above example shows you can use Consumer directly in JSX, is the main advantage readability?
Also why is there 2 MediaQuery.Consumer
?
Just want to be careful about adding too many different ways to use react-media-query-hoc
also providing a non-hoc mechanism might mean ill need to rename repo 😆 (which isnt the end of the world if there is a good use case for it)
from react-media-query-hoc.
@jooj123 Updated the example earlier. There should only be one MediaQuery.Consumer in there.
With the new Context API, you wrap your component tree with the Provider and below that, when you need access to the Context data, you wrap your component with a Consumer, which then takes a Function-as-Children component and passes the data to that, and then you can pass it down as props. I updated the example above to show the correct usage.
Here's an example I made to try out state management with the new Context API. You can see the Consumer usage in Form.js: https://codesandbox.io/s/l5y6kom6mq
It's interesting, though. The thing that attracted me to this library (other than not tying media query usage simply to conditional rendering and being recently maintained) is that it offered multiple ways to use it. I prefer not to use HOCs personally, so the Provider works great for me.
from react-media-query-hoc.
@jooj123 Here's a prototype of it with the new Context API: https://codesandbox.io/s/1vvm326qp7
There's no change to the HOC usage.
If someone was using the provider without the HOC and consuming directly from this.context (like I've been doing), then it could break (depending on their version of React). Either we can advise them to use the MediaQueryContextConsumer FAC component that's exported from media-query-context.js, or we can provide a separate abstracted one that does the same thing (in case of any potential future API changes). I added an example of that in there also.
Let me know what you think.
from react-media-query-hoc.
Looks good 🎉 - throw up the PR for review, I might get a few 👀 onto it
from react-media-query-hoc.
Done - closing
from react-media-query-hoc.
Related Issues (14)
- Allow passing of values to matchmedia for Server Side Rendering HOT 1
- Context is not updated when state changes HOT 1
- Prevent MediaQueryProvider from printing any additional div HOT 5
- Move to latest React, enzyme and babel HOT 1
- Add e2e test framework HOT 2
- Setup with nwb
- State being updated on window resize, context isn't. HOT 5
- Changelog for v1.0 ? HOT 1
- Add CI to the repo
- withMedia hoc can cause jumpy behaviour HOT 2
- Update to Babel 7
- Fix Travis badge
- Setup semantic release
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-media-query-hoc.