Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
rescript-react
Overview
  • Introduction
  • Installation
  • Migrate from JSX v3
Main Concepts
  • Elements & JSX
  • Rendering Elements
  • Components and Props
  • Arrays and Keys
  • Refs and the DOM
  • Context
  • Styling
  • Router
Hooks & State Management
  • Hooks & State Management Overview
  • useEffect Hook
  • useState Hook
  • useReducer Hook
  • useContext Hook
  • useRef Hook
  • Build A Custom Hook
Guides
  • Beyond JSX
  • Forwarding Refs
  • Extensions of props
    • Spread props
    • Shared props
Docs / rescript-react / Extensions of props
Edit

Extensions of Props

Note: This page assumes your bsconfig.json to be set to "jsx": { "version": 4 } to apply the right JSX transformations.

Spread props

JSX props spread is supported now, but in a stricter way than in JS.

ReScriptJS Output
<Comp {...props} a="a" />

Multiple spreads are not allowed:

RES
<NotAllowed {...props1} {...props2} />

The spread must be at the first position, followed by other props:

RES
<NotAllowed a="a" {...props} />

Shared props

You can control the definition of the props type by passing as argument to @react.component the body of the type definition of props. The main application is sharing a single type definition across several components. Here are a few examples:

DecoratedExpanded
type sharedprops<'x, 'y> = {x: 'x, y: 'y, z:string}

module C1 = {
  @react.component(:sharedProps<'a, 'b>)
  let make = (~x, ~y) => React.string(x ++ y ++ z)
}

module C2 = {
  @react.component(:sharedProps<string, 'b>)
  let make = (~x, ~y) => React.string(x ++ y ++ z)
}

module C3 = {
  type myProps = sharedProps<int, int>
  @react.component(:myProps)
  let make = (~x, ~y) => React.int(x + y)
}

This feature can be used when the nominally different components are passed to the prop of Screen component in ReScript React Native Navigation.

RES
type screenProps = { navigation: navigation, route: route } module Screen: { @react.component let make: ( ~name: string, ~component: React.component<screenProps>, ... ) => React.element } <Screen name="SomeScreen" component={A.make} // This will cause a type check error ... /> <Screen name="SomeScreen" component={B.make} // This will cause a type check error ... />

This example cann't pass the type checker, because the props types of both components are nominally different. You can make the both components having the same props type by passing screenProps type as argument to @react.component.

RES
module A = { @react.component(:screenProps) let make = ( ~navigation: navigation, ~route: route ) => ... } module B = { @react.component(:screenProps) let make = ( ~navigation: navigation, ~route: route ) => ... }
Forwarding Refs

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on