3

How can I add a button to a component using higher order component? I tried this but its not adding the button inside the component. Its adding it before the original component.

const withButton = WrappedComponent => {
  return class extends Component {
    render() {
      return (
        <Fragment>
          <button>BUTTON ADDED USING HOC</button>
          <WrappedComponent {...this.props} />
        </Fragment>
      )
    }
  }
}
export default withButton

When I call the HOF like this

const ComponentWithButton = withButton(WrappedComponent)

ComponentWithButton has button added but its adding before WrappedComponent whereas I want to add button inside as a child of the WrappedComponent.

Lets say that WrappedComponent is rendering something like

<div className="baseClass">{other divs and layout}</div>

const ComponentWithButton = withButton(WrappedComponent)

ComponentWithButton should render the following

<div className="baseClass">
<button>BUTTON ADDED USING HOC</button>
{other divs and layout}
</div>

5
  • Please, clarify what do you mean by 'inside'. As a child? What should happen with WrappedComponent's own children? Please add an example for WrappedComponent and how you expect the layout to be modified by withButton. Commented May 5, 2019 at 10:09
  • Hi estus, yes i want button to be added inside the wrapped component as a child. The wrapped component already has basic styling and I just want to add button which will also be a part of the wrapped component when necessary Commented May 5, 2019 at 10:12
  • Did you try putting the button inside wrapped component ? <Fragment> <WrappedComponent {...this.props}> <button>BUTTON ADDED USING HOC</button> </WrappedComponent> </Fragment> Commented May 5, 2019 at 10:16
  • 1
    This still depends on WrappedComponent. Does <WrappedComponent {...this.props}><button>...</button></WrappedComponent> do what you want? Please, provide an example in case it doesn't. Commented May 5, 2019 at 10:16
  • If I add button inside the <WrappedComponent />, <WrappedComponent /> will only have button side Commented May 5, 2019 at 10:24

3 Answers 3

1

Try using props.children, also refer to React.Children API

function ComponentWithButton({ children }) {
  return (
    <>
      <button>BUTTON ADDED USING HOC</button>
      {children}
    </>
  );
}

And then render:

<ComponentWithButton>
  <WrappedComponent />
</ComponentWithButton>

With classes:

class ComponentWithButton extends Component {
  render() {
    const { children } = this.props;
    return (
      <>
        <button>BUTTON ADDED USING HOC</button>
        {children}
      </>
    );
  }
}
export default ComponentWithButton;
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to dynamically place the button somewhere inside the WrappedComponent, you can try something like this.

const withButton = WrappedComponent => {
  return class extends Component {
    render() {
      return (
        <Fragment>
          <WrappedComponent {...this.props}>
            <button>BUTTON ADDED USING HOC</button>
          </WrappedCompnent>
        </Fragment>
      )
    }
  }
}
export default withButton

Now in your wrapped component, you can place the button any where you want as the button would be accessible as a property children to WrappedComponent.

const WrappedComponent = ({ children, ...otherProps }) => (
  <div className="baseClass">
    {children}
    {renderOtherDivsAndProps(otherProps)}
  </div>
);

Hope this helps you

Comments

0

I tried this and I am getting what I am looking for.

const withButton = WrappedComponent => {
  return class extends Component {
    render() {
      return (
          <WrappedComponent {...this.props}>
            <button>BUTTON ADDED USING HOC</button>
            {this.props.children}
          </Wrappedcomponent>
      )
    }
  }
}
export default withButton

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.