From 04f36ee436eb0d600e352794cd8a762cd8c9c4f7 Mon Sep 17 00:00:00 2001 From: John DeSilva Date: Sun, 11 Sep 2016 13:31:02 -0400 Subject: [PATCH 1/2] Remove unexpected tab character --- example.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.jsx b/example.jsx index 7c78736..08b7224 100644 --- a/example.jsx +++ b/example.jsx @@ -17,7 +17,7 @@ class MyComponent extends React.Component { }}> {`${this.props.containerWidth}px x ${this.props.containerHeight}px`} - ) + ) } } From 74793315e386ff33da4566dbe877a98b95dc7f74 Mon Sep 17 00:00:00 2001 From: John DeSilva Date: Sun, 11 Sep 2016 13:31:07 -0400 Subject: [PATCH 2/2] Optionally disable wrappedInstance ref Stateless components cannot use refs, so this adds the withRef option (enabled by default for compatibility) which enables or disables passing the wrappedInstance ref to the wrapped component. Addresses issue #30. Inspired by https://github.com/reactjs/react-redux/commit/2d3d0beade55477b3af65534ceb793db18b25705 --- README.md | 2 ++ index.jsx | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4de80cf..de8a9ee 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ or as an [ES7 class decorator](https://github.com/wycats/javascript-decorators) - `options.className` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)=** Control the class name set on the wrapper `
` - `options.elementResize` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)=** Set true to watch the wrapper `div` for changes in size which are not a result of window resizing - e.g. changes to the flexbox and other layout. (optional, default `false`) + - `options.withRef` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)=** Set true to enable accessing the wrapped instance with + [getWrappedInstance()](https://github.com/digidem/react-dimensions#getwrappedinstance). Disable this when wrapping stateless components, as they cannot use `refs`. (optional, default `true`) **Examples** diff --git a/index.jsx b/index.jsx index 35f3ae5..6cc4faa 100644 --- a/index.jsx +++ b/index.jsx @@ -77,7 +77,8 @@ module.exports = function Dimensions ({ getDimensions = defaultGetDimensions, debounce = 0, debounceOpts = {}, - elementResize = false + elementResize = false, + withRef = true } = {}) { return (ComposedComponent) => { return class DimensionsHOC extends React.Component { @@ -152,6 +153,9 @@ module.exports = function Dimensions ({ * @return {object} The rendered React component **/ getWrappedInstance () { + if (!withRef) { + console.warn('You need to enable the `withRef` option to access the wrapped instance') + } return this.refs.wrappedInstance } @@ -173,7 +177,7 @@ module.exports = function Dimensions ({ {...this.state} {...this.props} updateDimensions={this.updateDimensions} - ref='wrappedInstance' + ref={withRef && 'wrappedInstance'} /> }