Skip to content

Commit 8afa0a2

Browse files
jonthysellchrisglein
authored and
msftbot[bot]
committed
Native Module Setup Guide (microsoft#3628)
* Native Module Setup Guide * Added NativeModuleSetup.md, microsoft#3623 * Added link to new guide in NativeModules.md, ViewManagers.md * Updated ProjectStructure.md with Microsoft.ReactNative projects * Removed PropertySheets in SampleApps and CLI solutions * Change files * Apply suggestions from code review Co-Authored-By: Chris Glein <[email protected]>
1 parent 46578c1 commit 8afa0a2

File tree

10 files changed

+204
-46
lines changed

10 files changed

+204
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Native Module Setup Guide * Added NativeModuleSetup.md, #3623 * Added link to new guide in NativeModules.md, ViewManagers.md * Updated ProjectStructure.md with Microsoft.ReactNative projects * Removed PropertySheets in SampleApps and CLI solutions",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"commit": "91fc720c9220848107206d8a881ea269d444021f",
7+
"date": "2019-11-08T23:42:24.452Z"
8+
}

packages/microsoft-reactnative-sampleapps/windows/SampleApps.sln

-13
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactWindowsCore", "..\..\.
2626
{A990658C-CE31-4BCC-976F-0FC6B1AF693D} = {A990658C-CE31-4BCC-976F-0FC6B1AF693D}
2727
EndProjectSection
2828
EndProject
29-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PropertySheets", "PropertySheets", "{6F24927E-EE45-4DB2-91DA-DCC6E98B0C42}"
30-
ProjectSection(SolutionItems) = preProject
31-
..\..\..\vnext\PropertySheets\ARM.props = ..\..\..\vnext\PropertySheets\ARM.props
32-
..\..\..\vnext\PropertySheets\Debug.props = ..\..\..\vnext\PropertySheets\Debug.props
33-
..\..\..\vnext\PropertySheets\React.Cpp.props = ..\..\..\vnext\PropertySheets\React.Cpp.props
34-
..\..\..\vnext\PropertySheets\Release.props = ..\..\..\vnext\PropertySheets\Release.props
35-
..\..\..\vnext\PropertySheets\Warnings.props = ..\..\..\vnext\PropertySheets\Warnings.props
36-
..\..\..\vnext\PropertySheets\Win32.props = ..\..\..\vnext\PropertySheets\Win32.props
37-
..\..\..\vnext\PropertySheets\x64.props = ..\..\..\vnext\PropertySheets\x64.props
38-
..\..\..\vnext\PropertySheets\x86.props = ..\..\..\vnext\PropertySheets\x86.props
39-
EndProjectSection
40-
EndProject
4129
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Chakra", "..\..\..\vnext\Chakra\Chakra.vcxitems", "{C38970C0-5FBF-4D69-90D8-CBAC225AE895}"
4230
EndProject
4331
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.ReactNative", "..\..\..\vnext\Microsoft.ReactNative\Microsoft.ReactNative.vcxproj", "{F7D32BD0-2749-483E-9A0D-1635EF7E3136}"
@@ -234,7 +222,6 @@ Global
234222
{2D5D43D9-CFFC-4C40-B4CD-02EFB4E2742B} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
235223
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
236224
{11C084A3-A57C-4296-A679-CAC17B603144} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
237-
{6F24927E-EE45-4DB2-91DA-DCC6E98B0C42} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
238225
{C38970C0-5FBF-4D69-90D8-CBAC225AE895} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
239226
{F7D32BD0-2749-483E-9A0D-1635EF7E3136} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
240227
{0CC28589-39E4-4288-B162-97B959F8B843} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}

vnext/docs/GettingStarted.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a summary of setup steps needed to install and work with React Native for Windows (vnext). See the [React Native Getting Started Guide](http://facebook.github.io/react-native/docs/getting-started.html) for React Native details and see [Getting Started Guide - current](https://github.com/microsoft/react-native-windows/blob/master/current/docs/GettingStarted.md) for working with the `current` React Native for Windows implementation.
44

5-
## System requirements
5+
## System Requirements
66
* You can run React-Native for Windows10 apps only on Windows 10 devices and Windows version: 10.0.15063.0 or higher. See [Windows 10 Compatability](./win10compat.md) for version support details.
77
* Ensure Developer Mode is turned ON in Windows Settings App.
88
* [Visual Studio 2019](https://www.visualstudio.com/downloads) with the following options:

vnext/docs/NativeModules.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Native Modules and React Native Windows
22

3-
*Both this documentation and the underlying code is a work in progress. You can see the current state of working code here: [packages/microsoft-reactnative-sampleapps](../../packages/microsoft-reactnative-sampleapps)*
3+
>**This documentation and the underlying platform code is a work in progress. You can see the current state of working code here: [packages/microsoft-reactnative-sampleapps](../../packages/microsoft-reactnative-sampleapps)**
44
55
Sometimes an app needs access to a platform API that React Native doesn't have a corresponding module for yet. Maybe you want to reuse some existing .NET code without having to reimplement it in JavaScript, or write some high performance, multi-threaded code for image processing, a database, or any number of advanced extensions.
66

@@ -22,6 +22,20 @@ React Native for Windows supports authoring native modules in both C# and C++. E
2222

2323
> NOTE: If you are unable to use the reflection-based annotation approach, you can define native modules directly using the ABI. This is outlined in the [Native Modules and React Native Windows (Advanced Topics)](./NativeModulesAdvanced.md) document.
2424
25+
## Initial Setup
26+
27+
This guide assumes you already have the development environment and project structure set up for authoring native modules and are ready to write code.
28+
29+
If you are only planning on adding a native module to your existing React Native Windows app, ie:
30+
31+
1. You followed [Consuming react native windows](./ConsumingRNW.md), where
32+
1. You ran `react-native windows --template vnext` to add Windows to your project, and
33+
1. You are just adding your native code to the app project under the `windows` folder.
34+
35+
Then you can simply open the Visual Studio solution in the `windows` folder and add the new files directly to the app project.
36+
37+
If you are instead creating a standalone native module, or adding Windows support to an existing native module, check out the [Native Modules Setup](./NativeModulesSetup.md) guide first.
38+
2539
## Sample Native Module (C#)
2640

2741
### Attributes
@@ -34,7 +48,6 @@ React Native for Windows supports authoring native modules in both C# and C++. E
3448
| `ReactConstantProvider` | Specifies a method that provides a set of constants. |
3549
| `ReactEvent` | Specifies a field or property that represents an event. |
3650

37-
3851
### 1. Authoring your Native Module
3952

4053
Here is a sample native module written in C# called `FancyMath`. It is a simple class that defines two numerical constants and a method 'add'.

vnext/docs/NativeModulesAdvanced.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Native Modules and React Native Windows (Advanced Topics)
22

3-
*Both this documentation and the underlying code is a work in progress. You can see the current state of working code here: [packages/microsoft-reactnative-sampleapps](../../packages/microsoft-reactnative-sampleapps)*
3+
>**This documentation and the underlying platform code is a work in progress. You can see the current state of working code here: [packages/microsoft-reactnative-sampleapps](../../packages/microsoft-reactnative-sampleapps)**
44
55
## Writing Native Modules without using Attributes (C#)
66

vnext/docs/NativeModulesSetup.md

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Native Module Setup
2+
3+
>**This documentation and the underlying platform code is a work in progress. You can see the current state of working code here: [packages/microsoft-reactnative-sampleapps](../../packages/microsoft-reactnative-sampleapps)**
4+
5+
This guide will help set you up with the Visual Studio infrastructure to author your own stand-alone native module for React Native Windows. In this document we'll be creating the scaffolding for a `MyLibrary` native module.
6+
7+
## Development Environment
8+
9+
Install the tools specified in the *System Requirements* and *Dependencies* sections of [Getting Started Guide for React Native Windows (vnext)](./GettingStarted.md).
10+
11+
If you're planning on writing in C++, you'll also want to download and install the [C++/WinRT Visual Studio Extension](https://marketplace.visualstudio.com/items?itemName=CppWinRTTeam.cppwinrt101804264).
12+
13+
## New Project (optional)
14+
15+
If you already have an existing native module project for iOS/Android, you can skip straight to [Manually Adding Windows Support](#manually-adding-windows-support).
16+
17+
Otherwise, if you're creating a new project from scratch, the quickest way is to follow the official React Native instructions at https://facebook.github.io/react-native/docs/native-modules-setup.
18+
19+
```cmd
20+
yarn global add create-react-native-module
21+
create-react-native-module MyLibrary
22+
```
23+
24+
Now you'll have a new native module project under `react-native-my-library`. Be sure to look at the command output for further steps you'll want to do before publishing the project.
25+
26+
## Manually Adding Windows Support
27+
28+
>**The plan is to automate this process as part of a CLI new library project template, see issues [3201](https://github.com/microsoft/react-native-windows/issues/3201) and [3203](https://github.com/microsoft/react-native-windows/issues/3203). However we are also documenting the manual process here for developers who are unable to use the CLI.**
29+
30+
### Updating your package.json
31+
32+
In the directory for your native module project, you first need to update to `react-native` 0.60 and get the latest `react-native-windows`:
33+
34+
```cmd
35+
yarn add [email protected] --dev
36+
yarn add react-native-windows@vnext --peer
37+
```
38+
39+
Next you'll want to create a `windows` subdirectory to hold the windows code:
40+
41+
```cmd
42+
mkdir windows
43+
```
44+
45+
Now it's time to switch into Visual Studio and create a new project.
46+
47+
### Creating the Visual Studio Project / Solution
48+
49+
Open Visual Studio and select `Create a new project`. You're going to create a new `Windows Runtime Component` project, which produce a library that is compatible with Windows UWP apps.
50+
51+
If you're planning on writing your native module in C#, you'll want to choose `Windows Runtime Component (Universal Windows)`.
52+
53+
If you're planning on writing your native module in C++, you'll want to choose `Windows Runtime Component (C++/WinRT)`. Note, if you don't see that project type, make sure you installed the *C++/WinRT Visual Studio Extension* linked to above under [Development Environment](#development-environment).
54+
55+
1. Set the `Project Name` to `MyLibrary`.
56+
1. Set the `Location` to the path of the `windows` subdirectory you created earlier.
57+
1. Set the `Solution Name` to `MyLibrary`.
58+
1. Click `Create`.
59+
60+
Next you'll be prompted to select the versions of Windows you'll support. This should match the values for React Native Windows:
61+
62+
1. Set the `Target version` to `Windows 10, version 1903 (10.0; Build 18362)`.
63+
1. Set the `Minimum version` to `Windows 10 Creators Update (10.0; Build 15063)`.
64+
65+
You'll now have a new `MyLibrary` solution file at `windows\MyLibrary.sln` and a `MyLibrary` project under `windows\MyLibrary\`. Now it's time to add React Native Windows into the solution.
66+
67+
### Adding React Native Windows to the Visual Studio Solution
68+
69+
We're going to add several React Native Windows projects to your solution. So to avoid confusing them with your own code, we're first going to create a solution folder called `ReactNative`:
70+
71+
1. Open the Solution Explorer sidebar.
72+
1. Right-click on `Solution 'MyLibrary'` at the top.
73+
1. Select `Add` > `New Solution Folder`.
74+
1. Name the folder `ReactNative`.
75+
76+
Now we're going to add all of the following React Native Windows projects to that `ReactNative` folder. All of these projects are located under the `node_modules\react-native-windows` directory in the root of your `react-native-my-library` project directory.
77+
78+
>*For more details about what these projects do, see [Project Structure](ProjectStructure.md).*
79+
80+
| VS Project | Project File |
81+
|:-----------|:-------------|
82+
| Chakra | `Chakra\Chakra.vcxitems` |
83+
| Common | `Common\Common.vcxproj` |
84+
| Folly | `Folly\Folly.vcxproj` |
85+
| JSI.Shared | `JSI\Shared\JSI.Shared.vcxitems` |
86+
| JSI.Universal | `JSI\Universal\JSI.Universal.vcxproj` |
87+
| Microsoft.ReactNative | `Microsoft.ReactNative\Microsoft.ReactNative.vcxproj` |
88+
| Microsoft.ReactNative.Cxx | `Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems` |
89+
| Microsoft.ReactNative.SharedManaged | `Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.shproj` |
90+
| ReactCommon | `ReactCommon\ReactCommon.vcxproj` |
91+
| ReactUWP | `ReactUWP\ReactUWP.vcxproj` |
92+
| ReactWindowsCore | `ReactWindowsCore\ReactWindowsCore.vcxproj` |
93+
94+
For each project, you'll do the following:
95+
96+
1. Open the Solution Explorer sidebar.
97+
1. Right-click on the `ReactNative` folder.
98+
1. Select `Add` > `Existing Project...`.
99+
1. Select the project file and click `Open`.
100+
101+
You now have all of the React Native Windows projects to your solution. Next we're going to reference them in our `MyLibrary` project.
102+
103+
### Referencing React Native Windows in your Project
104+
105+
The only project reference you **must** add is `Micrsoft.ReactNative`. To add the reference:
106+
107+
1. Open the Solution Explorer sidebar.
108+
1. Right-click on your `MyLibrary` project.
109+
1. Select `Add` > `Reference`.
110+
1. Select `Projects` on the left-hand side.
111+
1. Check the box next to `Microsoft.ReactNative`.
112+
1. Click `OK`.
113+
114+
After you've added the reference, you need to make sure it doesn't copy itself into your build (otherwise it'll cause build conflicts down the line when you're trying on consume your library):
115+
116+
1. Open the Solution Explorer sidebar.
117+
1. Under your `MyLibrary` project, expand the `References`.
118+
1. Right-click on `Microsoft.ReactNative`.
119+
1. Select `Properties`.
120+
1. Under `Build`, Change `Copy Local` to `False`.
121+
122+
Now, you're technically ready to go, but in order to improve the developer experience, it's also **highly recommended** to also add a reference to the appropriate helper shared project. These projects contain the attributes (C#) and macros (C++) as described in the [Native Modules](./NativeModules.md) and [View Managers](./ViewManagers.md).
123+
124+
If you're writing in C#, you'll want to add `Microsoft.ReactNative.SharedManaged`:
125+
126+
1. Open the Solution Explorer sidebar.
127+
1. Right-click on your `MyLibrary` project.
128+
1. Select `Add` > `Reference`.
129+
1. Select `Shared Projects` on the left-hand side.
130+
1. Check the box next to `Microsoft.ReactNative.SharedManaged`.
131+
1. Click `OK`.
132+
133+
If you're writing in C++, you'll want to add `Microsoft.ReactNative.Cxx`:
134+
135+
1. Open the Solution Explorer sidebar.
136+
1. Right-click on your `MyLibrary` project.
137+
1. Select `Add` > `Reference`.
138+
1. Select `Shared Projects` on the left-hand side.
139+
1. Check the box next to `Microsoft.ReactNative.Cxx`.
140+
1. Click `OK`.
141+
142+
### Testing your Build
143+
144+
To make sure that everything is working, you'll want to try building `MyLibrary`. First you'll want to make sure you've chosen a supported platform:
145+
146+
1. At the top, change the `Solution Platform` to `x86`, `x64` or `ARM`.
147+
1. In the `Build` menu, select `Build Solution`.

vnext/docs/ProjectStructure.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ Sample applications are not covered.
1818
- [JSI\Shared\JSI.Shared.vcxitems](#JSI.Shared)
1919
- [JSI\Universal\JSI.Universal.vcxproj](#JSI.Universal)
2020
- [JSI.Desktop.UnitTests\JSI.Desktop.UnitTests](#JSI.Desktop.UnitTests)
21+
- **[Microsoft.ReactNative\Microsoft.ReactNative.vcxproj](#Microsoft.ReactNative)**
22+
- [Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems](#Microsoft.ReactNative.Cxx)
23+
- [Microsoft.ReactNative.SharedManaged\Microsoft.ReactNative.SharedManaged.projitems](#Microsoft.ReactNative.SharedManaged)
2124
- [ReactCommon\ReactCommon.vcxproj](#ReactCommon)
22-
- **[ReactUWP\ReactUWP.vcxproj](#ReactUWP)**
25+
- [ReactUWP\ReactUWP.vcxproj](#ReactUWP)
2326
- [ReactWindowsCore\ReactWindowsCore.vcxproj](#ReactWindowsCore)
2427
- [Shared\Shared.vcxitems](#Shared)
2528
- [Universal.IntegrationTests\React.Windows.Universal.IntegrationTests.vcxproj](#React.Windows.Universal.IntegrationTests)
@@ -59,6 +62,14 @@ ChakraCore bridging layer. May use different compiler flags between Windows vari
5962
*Shared Items (no build artifact)*<br/>
6063
Code shared between [JSI\Desktop\JSI.Desktop.vcxproj](#JSI.Desktop) and [JSI\Universal\JSI.Universal.vcxproj](#JSI.Universal).
6164

65+
### Microsoft.ReactNative.Cxx
66+
*Shared Items (no build artifact)*<br/>
67+
Contains helpers to simplify authoring C++/WinRT native modules on top of [Microsoft.ReactNative](#Microsoft.ReactNative).
68+
69+
### Microsoft.ReactNative.SharedManaged
70+
*Shared Items (no build artifact)*<br/>
71+
Contains helpers to simplify authoring C# native modules on top of [Microsoft.ReactNative](#Microsoft.ReactNative).
72+
6273
### React.Windows.IntegrationTests
6374
*Static Library*<br/>
6475
Common framework for running out of process and/or full React instance testing.
@@ -105,10 +116,14 @@ Sources provided as part of the `react-native` Node dependency. Not part of this
105116

106117
## Windows Universal Projects
107118

119+
### Microsoft.ReactNative
120+
*Windows Runtime Component*</br>
121+
The primary Windows Universal entry point and public API surface for React Native Windows. Currently depends on the implementation details in [ReactUWP](#ReactUWP).<br/>
122+
**Main artifact to use in Windows Universal applications.**
123+
108124
### ReactUWP
109125
*Dynamic Library*<br/>
110-
Set of Native Modules, View Managers and Executors for Windows Universal.<br/>
111-
**Main artifact to use in Windows Universal applications.**
126+
Set of Native Modules, View Managers and Executors for Windows Universal. Formerly the entry point and ABI surface for React Native Windows, it will eventually be subsumed by [Microsoft.ReactNative](#Microsoft.ReactNative).
112127

113128
### React.Windows.Universal.UnitTests
114129
*VSTest Dynamic Library*

vnext/docs/ViewManagers.md

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ Similarly to authoring native modules, at a high level you must:
1414
2. Register your new ViewManager within the native code of your React Native host application.
1515
3. Reference the new Component within your React Native JSX code.
1616

17+
## Initial Setup
18+
19+
This guide assumes you already have the development environment and project structure set up for authoring native modules and are ready to write code.
20+
21+
If you are only planning on adding a native module to your existing React Native Windows app, ie:
22+
23+
1. You followed [Consuming react native windows](./ConsumingRNW.md), where
24+
1. You ran `react-native windows --template vnext` to add Windows to your project, and
25+
1. You are just adding your native code to the app project under the `windows` folder.
26+
27+
Then you can simply open the Visual Studio solution in the `windows` folder and add the new files directly to the app project.
28+
29+
If you are instead creating a standalone native module, or adding Windows support to an existing native module, check out the [Native Modules Setup](./NativeModulesSetup.md) guide first.
30+
1731
## Sample ViewManager (C#)
1832

1933
### Attributes

vnext/local-cli/generator-windows/templates/cpp/proj/MyApp.sln

-13
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ReactWindowsCore", "..\node
2525
{A990658C-CE31-4BCC-976F-0FC6B1AF693D} = {A990658C-CE31-4BCC-976F-0FC6B1AF693D}
2626
EndProjectSection
2727
EndProject
28-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PropertySheets", "PropertySheets", "{6F24927E-EE45-4DB2-91DA-DCC6E98B0C42}"
29-
ProjectSection(SolutionItems) = preProject
30-
PropertySheets\ARM.props = PropertySheets\ARM.props
31-
PropertySheets\Debug.props = PropertySheets\Debug.props
32-
PropertySheets\React.Cpp.props = PropertySheets\React.Cpp.props
33-
PropertySheets\Release.props = PropertySheets\Release.props
34-
PropertySheets\Warnings.props = PropertySheets\Warnings.props
35-
PropertySheets\Win32.props = PropertySheets\Win32.props
36-
PropertySheets\x64.props = PropertySheets\x64.props
37-
PropertySheets\x86.props = PropertySheets\x86.props
38-
EndProjectSection
39-
EndProject
4028
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Chakra", "..\node_modules\react-native-windows\Chakra\Chakra.vcxitems", "{C38970C0-5FBF-4D69-90D8-CBAC225AE895}"
4129
EndProject
4230
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Microsoft.ReactNative", "..\node_modules\react-native-windows\Microsoft.ReactNative\Microsoft.ReactNative.vcxproj", "{F7D32BD0-2749-483E-9A0D-1635EF7E3136}"
@@ -310,7 +298,6 @@ Global
310298
{2D5D43D9-CFFC-4C40-B4CD-02EFB4E2742B} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
311299
{A9D95A91-4DB7-4F72-BEB6-FE8A5C89BFBD} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
312300
{11C084A3-A57C-4296-A679-CAC17B603144} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
313-
{6F24927E-EE45-4DB2-91DA-DCC6E98B0C42} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
314301
{C38970C0-5FBF-4D69-90D8-CBAC225AE895} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
315302
{F7D32BD0-2749-483E-9A0D-1635EF7E3136} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}
316303
{0CC28589-39E4-4288-B162-97B959F8B843} = {5EA20F54-880A-49F3-99FA-4B3FE54E8AB1}

0 commit comments

Comments
 (0)