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

[withWidth] Document the render prop #13074

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/src/pages/layout/breakpoints/RenderPropsWithWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import withWidth from '@material-ui/core/withWidth';
import Typography from '@material-ui/core/Typography';
import toRenderProps from 'recompose/toRenderProps';

const WithWidth = toRenderProps(withWidth());

function RenderPropsWithWidth() {
return (
<WithWidth>
{({ width }) => <Typography variant="subtitle1">Current width: {width}</Typography>}
</WithWidth>
);
}

export default RenderPropsWithWidth;
7 changes: 7 additions & 0 deletions docs/src/pages/layout/breakpoints/breakpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ In the following demo, we change the rendered DOM element (*em*, <u>u</u>, ~~del

⚠️ `withWidth()` server-side rendering support is limited.

### Render Props

In some cases, you could have property name collisions using higher-order components.
To avoid the issue, you can use the [render props](https://reactjs.org/docs/render-props.html) pattern like in the following demo.

{{"demo": "pages/layout/breakpoints/RenderPropsWithWidth.js"}}

## API

### `withWidth([options]) => higher-order component`
Expand Down
7 changes: 7 additions & 0 deletions pages/layout/breakpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ module.exports = require('fs')
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/layout/breakpoints/WithWidth'), 'utf8')
`,
},
'pages/layout/breakpoints/RenderPropsWithWidth.js': {
js: require('docs/src/pages/layout/breakpoints/RenderPropsWithWidth').default,
raw: preval`
module.exports = require('fs')
.readFileSync(require.resolve('docs/src/pages/layout/breakpoints/RenderPropsWithWidth'), 'utf8')
`,
},
}}
Expand Down