Skip to content

Commit

Permalink
Fix React Native error from function declaration hoisting
Browse files Browse the repository at this point in the history
It is unknown why relying upon function declaration hoisting causes an
error in React Native, but it does occur. Recent changes with WordPress#28676
refactored a class to a functional component, including leveraging
function declaration hoisting. I.e. the function was referenced
prior to its declaration in terms of the order of the lines of code.

Re-ordering the code so that the function declaration occurs prior to
referencing it appears to resolve the error in React Native.

Related: https://git.io/JtXMm
  • Loading branch information
dcalhoun committed Feb 15, 2021
1 parent 6ef9e82 commit 5135203
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/components/src/higher-order/with-notices/index.js
Expand Up @@ -22,15 +22,6 @@ import NoticeList from '../../notice/list';
* @return {WPComponent} Wrapped component.
*/
export default createHigherOrderComponent( ( OriginalComponent ) => {
let isForwardRef;
const { render } = OriginalComponent;
// Returns a forwardRef if OriginalComponent appears to be a forwardRef
if ( typeof render === 'function' ) {
isForwardRef = true;
return forwardRef( Component );
}
return Component;

function Component( props, ref ) {
const [ noticeList, setNoticeList ] = useState( [] );

Expand Down Expand Up @@ -97,4 +88,13 @@ export default createHigherOrderComponent( ( OriginalComponent ) => {
<OriginalComponent { ...propsOut } />
);
}

let isForwardRef;
const { render } = OriginalComponent;
// Returns a forwardRef if OriginalComponent appears to be a forwardRef
if ( typeof render === 'function' ) {
isForwardRef = true;
return forwardRef( Component );
}
return Component;
} );

0 comments on commit 5135203

Please sign in to comment.