Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Getting the named type of props for a React component

$
0
0

When you call @react.component, under the hood, it also declares a props type, which could be used elsewhere:

@react.component()
let make = (~x, ~y) => {
  <div>
    <div> {React.string("x: " ++ Int.toString(x))} </div>
    <div> {React.string("y: " ++ Int.toString(y))} </div>
  </div>
}

let makeParamsDoubled = ({x, y}: props<int, int>) => {
  x: x * 2,
  y: y * 2,
}

However, as you might have noticed, this type takes in a generic for each prop the component has, which can be cumbersome. As such, it might be easier to declare the type separately, and pass it to the component; the generated props will not need generics.

type myProps = {
  x: int,
  y: int
}

@react.component(:myProps)
let make = (~x, ~y) => {
  <div>
    <div> {React.string("x: " ++ Int.toString(x))} </div>
    <div> {React.string("y: " ++ Int.toString(y))} </div>
  </div>
}

let makeParamsDoubled = ({x, y}: props) => {
  x: x * 2,
  y: y * 2,
}

Viewing all articles
Browse latest Browse all 2592

Trending Articles