Skip to content

Instantly share code, notes, and snippets.

@keidrun
Created February 14, 2018 22:43
Show Gist options
  • Save keidrun/081c521b9e7b2afb21f17207738f3a6d to your computer and use it in GitHub Desktop.
Save keidrun/081c521b9e7b2afb21f17207738f3a6d to your computer and use it in GitHub Desktop.
React HOC examples
import React from 'react';
const Auth = props => {
const password = 'anyPassword';
if (password != 'correctPassword') {
return <h3>You are not authorized</h3>;
} else {
return props.children;
}
};
export default Auth;
import React from 'react';
const userHoc = (WrappedComponent, arg1) => {
return props => (
<div>
{arg1}
<WrappedComponent {...props} />
</div>
);
};
export default userHoc;
import React from 'react';
import Auth from '../hoc/auth';
const Page = props => {
return (
<div>
<Auth>
<SecretPage>Home</SecretPage>
</Auth>
</div>
);
};
export default Page;
import React from 'react';
import userHoc from '../hoc/userHoc';
const User = props => {
return <div>User</div>;
};
export default userHoc(User, 'Hello');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment