Skip to content

Instantly share code, notes, and snippets.

View sabesansathananthan's full-sized avatar
:octocat:
Work

Sathananthan Sabesan sabesansathananthan

:octocat:
Work
View GitHub Profile
@sabesansathananthan
sabesansathananthan / FilterProps.js
Created April 27, 2021 09:01
How React Components Are Reused
render() {
// Filter out extra props and don’t pass through
const { extraProp, ...passThroughProps } = this.props;
// Inject props into the wrapped component.
// Usually the value of state or an instance method.
const injectedProp = someStateOrInstanceMethod;
// Pass props to the Wrapped component
return (
@sabesansathananthan
sabesansathananthan / LogProps.js
Created April 25, 2021 04:41
How React Components Are Reused
function logProps(WrappedComponent) {
return class extends React.Component {
componentDidUpdate(prevProps) {
console.log("Current props: ", this.props);
console.log("Previous props: ", prevProps);
}
render() {
// Wrap the input component in a container without modifying it, Nice!
return <WrappedComponent {...this.props} />;
}
@sabesansathananthan
sabesansathananthan / LogProps.js
Created April 25, 2021 04:36
How React Components Are Reused
function logProps(InputComponent) {
InputComponent.prototype.componentDidUpdate = function(prevProps) {
console.log("Current props: ", this.props);
console.log("Previous props: ", prevProps);
};
// Return the original input component, which has been modified.
return InputComponent;
}
// Every time logProps is called, the enhanced component will have log output.
@sabesansathananthan
sabesansathananthan / HOCFactoryFactory.js
Created April 25, 2021 04:31
How React Components Are Reused
const HOCFactoryFactory = (params) => {
// Operation params here
return (WrappedComponent) => {
return class EnhancedComponent extends WrappedComponent {
render() {
return params.isRender && this.props.isRender && super.render();
}
}
}
}
@sabesansathananthan
sabesansathananthan / HOC.js
Created April 25, 2021 04:24
How React Components Are Reused
const HOC = (WrappedComponent) => {
return class EnhancedComponent extends WrappedComponent {
componentDidMount(){
// ...
}
render() {
return super.render();
}
}
}
@sabesansathananthan
sabesansathananthan / HOC.js
Created April 24, 2021 22:14
How React Components Are Reused
const HOC = (WrappedComponent) => {
return class EnhancedComponent extends WrappedComponent {
render() {
return this.props.isRender && super.render();
}
}
}
@sabesansathananthan
sabesansathananthan / HOC.js
Created April 23, 2021 12:24
How React Components Are Reused
const HOC = (WrappedComponent) => {
return class EnhancedComponent extends React.Component {
render() {
return (
<div class="layout">
<WrappedComponent {...this.props} />
</div>
);
}
}
@sabesansathananthan
sabesansathananthan / WrappedComponent.js
Created April 23, 2021 12:21
How React Components Are Reused
class WrappedComponent extends React.Component {
render() {
return <input name="name" />;
}
}
const HOC = (WrappedComponent) => {
return class EnhancedComponent extends React.Component {
constructor(props) {
super(props);
@sabesansathananthan
sabesansathananthan / HOC.js
Created April 23, 2021 12:04
How React Components Are Reused
const HOC = (WrappedComponent, store) => {
return class EnhancedComponent extends React.Component {
render() {
const newProps = {
id: store.id
}
return <WrappedComponent
{...this.props}
{...newProps}
/>;
@sabesansathananthan
sabesansathananthan / higherOrderComponent.js
Last active April 23, 2021 12:03
How React Components Are Reused
// High-order component definition
const higherOrderComponent = (WrappedComponent) => {
return class EnhancedComponent extends React.Component {
// ...
render() {
return <WrappedComponent {...this.props} />;
}
};
}