|
| 1 | +--- |
| 2 | +title: Upgrade from 2.x to 3.x |
| 3 | +displayed_sidebar: docsSidebar |
| 4 | +sidebar_position: 999 |
| 5 | +--- |
| 6 | + |
| 7 | +A lot fo F# wrapper magic was removed. React bindings now behave as close as possible to actual React functionality. |
| 8 | + |
| 9 | +## Update Fable Version |
| 10 | + |
| 11 | +Get the latest fable version (currently pre-release). |
| 12 | + |
| 13 | +```bash |
| 14 | +# cmd |
| 15 | +dotnet tool update fable --prerelease |
| 16 | +``` |
| 17 | + |
| 18 | +## Update .NET Framework |
| 19 | + |
| 20 | +Recommended is the use of .NET 8. You can check your local .NET version with `dotnet --version` and update your .fsproj files with |
| 21 | + |
| 22 | +```xml |
| 23 | +<TargetFramework>net8.0</TargetFramework> |
| 24 | +``` |
| 25 | + |
| 26 | +## React.memo |
| 27 | + |
| 28 | +`React.memo` behavior was reworked to improve compatibility with React devtools. It now **requires** to be called with `React.memoRenderer`. Check out the [docs](./api-docs/react/apis/memo) for more info and alternatives! |
| 29 | + |
| 30 | +```fsharp |
| 31 | +let MemoFunction = |
| 32 | + React.memo<{|text: string|}> (fun props -> |
| 33 | + // some component |
| 34 | + ) |
| 35 | +
|
| 36 | +[<ReactComponent>] |
| 37 | +let Main () = |
| 38 | + Html.div [ |
| 39 | + React.memoRender(MemoFunction, {| text = text |}) |
| 40 | + ] |
| 41 | +``` |
| 42 | + |
| 43 | +## React.lazy' |
| 44 | + |
| 45 | +`React.lazy'` behavior was reworked. It now **requires** to be called with `React.lazyRenderer`. Check out the [docs](./api-docs/react/apis/lazy) for more info and alternatives! |
| 46 | + |
| 47 | +```fsharp |
| 48 | +let LazyHello: LazyComponent<unit> = |
| 49 | + React.lazy'(fun () -> |
| 50 | + promise { |
| 51 | + do! Promise.sleep 2000 |
| 52 | + return! JsInterop.importDynamic "./Counter" |
| 53 | + } |
| 54 | + ) |
| 55 | +
|
| 56 | +[<ReactComponent>] |
| 57 | +let SuspenseDemo() = |
| 58 | + Html.div [ |
| 59 | + React.Suspense([ |
| 60 | + React.lazyRender(LazyHello, ()) |
| 61 | + ], |
| 62 | + Html.div [ prop.text "Loading..." ] |
| 63 | + ) |
| 64 | + ] |
| 65 | +``` |
| 66 | + |
| 67 | +## React.context |
| 68 | + |
| 69 | +Using context with React was reworked to more closely align with React functionality. |
| 70 | + |
| 71 | +```fsharp |
| 72 | +// Define a context for shared state |
| 73 | +// This can should be placed in a separate file for reuse |
| 74 | +let CounterContext = React.createContext(None: (int * (int -> unit)) option) |
| 75 | +
|
| 76 | +[<ReactComponent>] |
| 77 | +let CounterDisplay() = |
| 78 | + let ctx = React.useContext(CounterContext) |
| 79 | + match ctx with |
| 80 | + | Some(count, _) -> Html.p [ prop.text $"Current count: {count}" ] |
| 81 | + | None -> Html.p [ prop.text "No context available" ] |
| 82 | +
|
| 83 | +[<ReactComponent(true)>] |
| 84 | +let UseContext() = |
| 85 | + let count, setCount = React.useState(0) |
| 86 | + CounterContext.Provider( |
| 87 | + (Some (count, setCount)), |
| 88 | + CounterDisplay() |
| 89 | + ) |
| 90 | +
|
| 91 | +``` |
| 92 | + |
| 93 | +## FsReact |
| 94 | + |
| 95 | +All F# functions to help with React interop have been moved to FsReact namespace. |
| 96 | + |
| 97 | +* `FsReact.createDisposable` |
| 98 | +* `FsReact.useDisposable` |
| 99 | +* `FsReact.useCancellationToken` |
| 100 | + |
| 101 | +## Components use PascalCase |
| 102 | + |
| 103 | +According to React best practices, components are written in PascalCase instead of camelCase. This has been updated for React. |
| 104 | + |
| 105 | +* `React.Fragment` |
| 106 | +* `React.KeyedFragment` |
| 107 | +* `React.Imported` |
| 108 | +* `React.DynamicImported` |
| 109 | +* `React.StrictMode` |
| 110 | +* `React.Suspense` |
| 111 | +* `React.Provider` |
| 112 | +* `React.Consumer` |
0 commit comments