Pacote
    Preparing search index...
    • Creates a component that injects preset props into another component.

      The injected props may be an object or a function of the props supplied by the caller. Caller props are spread first, so injected values take precedence.

      Type Parameters

      • C extends ElementType
      • P extends object
      • I extends object = object

      Parameters

      • inject: P | Injector<C, P, I>

        Props to inject, or a function that derives them from caller props.

      • BaseComponent: C

        Component or DOM element type to enhance.

      Returns ComponentType<ExternalProps<C, P, I>>

      A component whose props exclude the injected keys.

      import { withProps } from '@pacote/react-with-props'

      type ComponentProps = { name: string; value: string }
      const NameValue = (props: ComponentProps) => (
      <div>{props.name}: {props.value}</div>
      )

      const ExampleValue = withProps({ name: 'Example' }, NameValue)
      render(<ExampleValue value="with props" />)
      // => <div>Example: with props</div>

      const FieldValue = withProps(
      ({ field = '' }) => ({ name: `[${field}]` }),
      NameValue,
      )
      render(<FieldValue field="Example" value="with props" />)
      // => <div>[Example]: with props</div>

      const PasswordInput = withProps({ type: 'password' }, 'input')
      render(<PasswordInput name="secret" />)
      // => <input type="password" name="secret" />