# conditionalize-component

> a react HOC for rendering components conditionally

Latest version **2.0.0** (published 2025-12-02) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install conditionalize-component
pnpm add conditionalize-component
yarn add conditionalize-component
bun add conditionalize-component
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-12-02 |
| First published | 2023-03-27 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 25.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Islam Yamor |
| Maintainers | samislam |
| Keywords | hoc, hook, react, hooks, render, condition, component, react-hook, conditional, conditionalize, react-component, useConditionals, conditional-hook, withConditionals, react-conditionalize, conditional-rendering, conditionalizecomponent, conditionalize-component, react-conditional-rendering |

## Links

- npm: https://www.npmjs.com/package/conditionalize-component
- Repository: https://github.com/samislam/conditionalize-component
- Homepage: https://github.com/samislam/conditionalize-component#readme
- Issues: https://github.com/samislam/conditionalize-component/issues
- npm.io page: https://npm.io/package/conditionalize-component

## Dependencies (3)

- [react](https://npm.io/package/react.md) ^19.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [common-es](https://npm.io/package/common-es.md) ^1.1.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2025-12-02
- 1.4.2 — 2024-09-29
- 1.4.1 — 2024-09-29
- 1.4.0 — 2024-09-29
- 1.3.0 — 2024-09-29
- 1.2.3 — 2024-09-28
- 1.2.2 — 2024-09-28
- 1.2.1 — 2024-09-28
- 1.2.0 — 2024-09-28
- 1.1.2 — 2024-02-02
- 1.1.1 — 2023-12-31
- 1.1.0 — 2023-12-31
- 1.0.6 — 2023-03-28
- 1.0.5 — 2023-03-28
- 1.0.4 — 2023-03-28
- … 4 more at https://npm.io/package/conditionalize-component/versions

## README

# Conditionalize-component

a powerful react HOC (Higher Order Component) for rendering components conditionally quickly

# Installation:

```bash
$ npm i conditionalize-component
```

# Example:

You can use `conditionalize-component` with **any** react function component.

```tsx
import { Box } from 'MyBoxComponent'
import { withConditionals } from 'conditionalize-component'

const BoxC = withConditionals(Box) // now BoxC is wrapped with the conditional props such as renderIf

function UsersPage() {
  return (
    <div>
      <h1>Other stuff</h1>
      <BoxC renderIf={true}>You'll see me</BoxC>
      <BoxC renderIf={false}>You'll not see me</BoxC>
    </div>
  )
}
```

## Example usage on HTML elements:

For native html elements, or Class components, simply wrap them in a function and pass them the necessary props

```tsx
import { withConditionals } from 'conditionalize-component'

export const Button = withConditionals((props: React.HTMLAttributes<HTMLButtonElement>) => <button {...props} />) // now button is wrapped with the conditional props such as renderIf

function UsersPage() {
  return (
    <div>
      <h1>Other stuff</h1>
      <Button renderIf={true}>You'll see me</Button>
      <Button renderIf={false}>You'll not see me</Button>
    </div>
  )
}
```

# Using the built-in `DivIf` component

You can also use the premade `DivIf` component which is basically a div but has the conditional props such as `renderIf`:
```tsx
import { DivIf } from 'conditionalize-component'

function App(){
 return (
    <div>
      <DivIf renderIf={1 + 1 === 2}> You'll see me if 1 + 1 = 2 </DivIf>
      <div> You'll see me when 5 + 5 = 12 </div>
    </div>
  )
}
```

# Using the built-in `RenderIf` component

If you don't want to use `DivIf` component because it adds an HTML element to your DOM, you can use the `RenderIf` component which is basically a react fragment but has the conditional props such as `renderIf`:
```tsx
import { RenderIf } from 'conditionalize-component'

function App(){
 return (
    <div>
      <RenderIf renderIf={1 + 1 === 2}> You'll see me if 1 + 1 = 2 </RenderIf>
      <div> You'll see me when 5 + 5 = 12 </div>
    </div>
 )
}
```

# API

After applying the HOC on any of your components, you'll get the following props added to the interface of your component props:

```ts
renderIf?: boolean (default `true`)
override?: ReactNode
fallback?: ReactNode
execludeChildren?: boolean (default: `false`)
```

## Explaination:

- `renderIf`: The simplest one, only render the component if the value of this prop is `true`.
- `override`: In case `renderIf` was `false`, and case `execludeChildren` was also set to `false`, render the react node provided in this prop.
- `fallback`: In case `renderIf` was `false`, render the react node provided to this prop instead as a fallback.
- `execludeChildren`: In case `renderIf` was `false`, only the children of the component will be rendered, excluding their parent.

---

Created by Islam Yamor.

---
_Source: https://npm.io/package/conditionalize-component · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
