|
| 1 | +# React API and JSX |
| 2 | + |
| 3 | +Most of the regular React API is integrated (like `React.createElement`), |
| 4 | +and the library uses a compile-time macro to parse JSX and generate |
| 5 | +the same kind of code that Facebook's JSX, Babel and Typescript will. |
| 6 | + |
| 7 | +```haxe |
| 8 | +import react.React; |
| 9 | +import react.ReactDOM; |
| 10 | +import react.ReactMacro.jsx; |
| 11 | +import Browser.document; |
| 12 | +
|
| 13 | +class App extends ReactComponent { |
| 14 | +
|
| 15 | + static public function main() { |
| 16 | + ReactDOM.render( |
| 17 | + jsx('<App/>'), |
| 18 | + document.getElementById('root') |
| 19 | + ); |
| 20 | + } |
| 21 | +
|
| 22 | + override function render() { |
| 23 | + var cname = 'it-bops'; |
| 24 | + return jsx(' |
| 25 | + <div className={cname}> |
| 26 | + <h1>Hello React</h1> |
| 27 | + </div> |
| 28 | + '); |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +Tips: |
| 34 | + |
| 35 | +- JSX has limitations, check the gotchas below, |
| 36 | +- Both classic JSX `{var}` binding and Haxe string interpolation are allowed: |
| 37 | + `attr=$var` / `${expression}` / `<$Comp>`. |
| 38 | + String interpolation can help for code completion/navigation. |
| 39 | +- Spread operator and complex expressions within curly braces are supported. |
| 40 | + |
| 41 | +Note: when writing externs, make sure to `extend ReactComponent` |
| 42 | + |
| 43 | +```haxe |
| 44 | +@:jsRequire('react-redux', 'Provider') |
| 45 | +extern class Provider extends ReactComponent { } |
| 46 | +``` |
| 47 | + |
| 48 | +### JSX gotchas |
| 49 | + |
| 50 | +1. JSX must be a String literal! |
| 51 | + **Do not concatenate Strings** to construct the JSX expression |
| 52 | + |
| 53 | +2. JSX parser is not "re-entrant" |
| 54 | + |
| 55 | + In JavaScript you can nest JSX inside curly-brace expressions: |
| 56 | + ```javascript |
| 57 | + return ( |
| 58 | + <div>{ isA ? <A/> : <B/> }</div> |
| 59 | + ); |
| 60 | + ``` |
| 61 | + |
| 62 | + However this isn't allowed in Haxe, so you must extract nested JSX into variables: |
| 63 | + ```haxe |
| 64 | + var content = isA ? jsx(<A/>) : jsx(<B/>); |
| 65 | + return jsx(<div>{content}</div>); |
| 66 | + ``` |
| 67 | + |
| 68 | +## Feature flags |
| 69 | + |
| 70 | +To control React features that should be enabled, depending on your target React version, |
| 71 | +use `-D react_ver=<version>`, like `-D react_ver=16.3` if you want to restrict to `16.3`. |
| 72 | + |
| 73 | +Otherwise all the features will be turned on: |
| 74 | + |
| 75 | +- `react_fragments`: e.g `<Fragment>`, since React 16.2 |
| 76 | +- `react_context_api`: e.g. `React.createContext`, since React 16.3 |
| 77 | +- `react_ref_api`: e.g. `React.createRef`, since React 16.3 |
| 78 | +- `react_snapshot_api`: e.g. `getSnapshotBeforeUpdate`, since React 16.3 |
| 79 | +- `react_unsafe_lifecycle`: e.g. `UNSAFE_componentWillMount`, since React 16.9 |
| 80 | + |
| 81 | +## Components strict typing |
| 82 | + |
| 83 | +The default `ReactComponent` type is a shorthand for |
| 84 | +`ReactComponentOf<Dynamic, Dynamic>`, a fully untyped component. |
| 85 | + |
| 86 | +To benefit from Haxe's strict typing you should look into extending a stricter base class: |
| 87 | + |
| 88 | +```haxe |
| 89 | +class ReactComponentOf<TProps, TState> {...} |
| 90 | +typedef ReactComponentOfProps<TProps> = ReactComponentOf<TProps, Empty>; |
| 91 | +typedef ReactComponentOfState<TState> = ReactComponentOf<Empty, TState>; |
| 92 | +``` |
| 93 | + |
| 94 | +The `Empty` type is an alias to `{}`, which means: |
| 95 | +- `ReactComponentOfProps` can NOT use any state, |
| 96 | +- `ReactComponentOfState` can NOT use any props. |
| 97 | + |
| 98 | +### Special case |
| 99 | + |
| 100 | +`componentDidUpdate` exceptionally doesn't need to be overriden with all its |
| 101 | +parameters, as it's common in JS to omit or add just what is needed: |
| 102 | +since React 16.3 you should normally exactly override the function as: |
| 103 | + |
| 104 | +```haxe |
| 105 | +override function componentDidUpdate(prevProps:Props, prevState:State, ?snapshot:Dynamic):Void { |
| 106 | + // ugh :( |
| 107 | +} |
| 108 | +
|
| 109 | +override function componentDidUpdate() { |
| 110 | + // nicer, and valid! |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Optimization |
| 115 | + |
| 116 | +### JSX compiler: inline ReactElements |
| 117 | + |
| 118 | +By default, when building for release (eg. without `-debug`), calls to `React.createElement` are replaced by inline JS objects (if possible). |
| 119 | + |
| 120 | +See: https://github.com/facebook/react/issues/3228 |
| 121 | + |
| 122 | +```javascript |
| 123 | +// regular |
| 124 | +return React.createElement('div', {key:'bar', className:'foo'}); |
| 125 | + |
| 126 | +// inlined (simplified) |
| 127 | +return {$$typeof:Symbol.for('react.element'), type:'div', props:{className:'foo'}, key:'bar'} |
| 128 | +``` |
| 129 | + |
| 130 | +This behaviour can be **disabled** using `-D react_no_inline`. |
| 131 | + |
| 132 | +## Warning for avoidable renders |
| 133 | + |
| 134 | +Setting `-D react_runtime_warnings` will enable runtime warnings for avoidable renders. |
| 135 | + |
| 136 | +This will add a `componentDidUpdate` (or update the existing one) where a |
| 137 | +**shallowCompare** is done on current and previous props and state. If both did |
| 138 | +not change, a warning will be displayed in the console. |
| 139 | + |
| 140 | +False positives can happen if your props are not flat, due to the shallowCompare. |
0 commit comments