2

Can someone please explain the difference between the functions build, buildU and buildSpec for React components in scalajs-react and when to use one over the other ?

user794783
  • 3,619
  • 7
  • 36
  • 58

2 Answers2

2

From https://github.com/japgolly/scalajs-react/blob/master/doc/USAGE.md

Call build (or buildU) and when it compiles you will have a React component. […] If your props type is Unit, use buildU instead to be able to instantiate your component with having to pass () as a constructor argument.

As for buildSpec, you can follow the types if you're interested but isn't mentioned because as @nafg says above, it's a low-level method that isn't relevant unless you already know what a "spec" is in the React world and you're doing something complicated with it. For what a React spec is, you can inspect the code and/or read about it in the React docs. Otherwise if you just want to create Scala React components, you only need to follow the instructions and examples and use build{,U}.

Golly
  • 1,319
  • 8
  • 18
1

Not 100% sure myself but it seems:

  • build produces a scala component function that takes an instance of your props type as a parameter
  • buildU is for when your props type is Unit (that's what the U stands for), i.e., you don't need any props object, so you can use the component without providing a props object.
  • buildSpec I'm less clear about, but it returns an instance of ReactComponentSpec which has a js.native annotation, so I suspect it somehow gives you lower-level access to the React object.
nafg
  • 2,424
  • 27
  • 25