@featbit/react-client-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +435 -0
- package/dist/asyncWithFbProvider.d.ts +29 -0
- package/dist/asyncWithFbProvider.js +129 -0
- package/dist/asyncWithFbProvider.js.map +1 -0
- package/dist/context.d.ts +15 -0
- package/dist/context.js +6 -0
- package/dist/context.js.map +1 -0
- package/dist/getFlagsProxy.d.ts +6 -0
- package/dist/getFlagsProxy.js +56 -0
- package/dist/getFlagsProxy.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/initClient.d.ts +12 -0
- package/dist/initClient.js +84 -0
- package/dist/initClient.js.map +1 -0
- package/dist/provider.d.ts +39 -0
- package/dist/provider.js +201 -0
- package/dist/provider.js.map +1 -0
- package/dist/types.d.ts +90 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/dist/useFbClient.d.ts +11 -0
- package/dist/useFbClient.js +18 -0
- package/dist/useFbClient.js.map +1 -0
- package/dist/useFlags.d.ts +11 -0
- package/dist/useFlags.js +16 -0
- package/dist/useFlags.js.map +1 -0
- package/dist/utils.d.ts +27 -0
- package/dist/utils.js +78 -0
- package/dist/utils.js.map +1 -0
- package/dist/withFbConsumer.d.ts +38 -0
- package/dist/withFbConsumer.js +36 -0
- package/dist/withFbConsumer.js.map +1 -0
- package/dist/withFbProvider.d.ts +25 -0
- package/dist/withFbProvider.js +50 -0
- package/dist/withFbProvider.js.map +1 -0
- package/package.json +57 -0
- package/src/asyncWithFbProvider.tsx +72 -0
- package/src/context.ts +24 -0
- package/src/getFlagsProxy.ts +82 -0
- package/src/index.ts +21 -0
- package/src/initClient.ts +26 -0
- package/src/provider.tsx +136 -0
- package/src/types.ts +103 -0
- package/src/useFbClient.ts +21 -0
- package/src/useFlags.ts +19 -0
- package/src/utils.ts +39 -0
- package/src/withFbConsumer.tsx +58 -0
- package/src/withFbProvider.tsx +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
# Featbit Client-Side SDK for ReactJS
|
|
2
|
+
|
|
3
|
+
## Introduction
|
|
4
|
+
|
|
5
|
+
This is the React client side SDK for the feature management platform [FeatBit](https://www.featbit.co).
|
|
6
|
+
|
|
7
|
+
Be aware, this is a client side SDK, it is intended for use in a single-user context, which can be mobile, desktop or embeded applications. This SDK can only be ran in a browser environment, it is not suitable for React Native projects, React Native SDK will be available in our other repo.
|
|
8
|
+
|
|
9
|
+
> The React SDK is based on the JavaScript SDK
|
|
10
|
+
The React SDK builds on FeatBit's JavaScript SDK to provide a better integration for use in React applications. As a result, much of the JavaScript SDK functionality is also available for the React SDK to use.
|
|
11
|
+
|
|
12
|
+
> SDK version compatibility
|
|
13
|
+
The React SDK is compatible with React version 16.3.0 and higher.
|
|
14
|
+
The React SDK offers two custom hooks. If you want to use these, then you must use React version 16.8.0 or higher. To learn more, read the section [Using Hooks](#using-hooks).
|
|
15
|
+
|
|
16
|
+
> Using with Next.js
|
|
17
|
+
If you are using this SDK with Next.js project, be aware that the SDK is not compatible with server-side rendering. You should use the SDK in the client side only.
|
|
18
|
+
If you want to use the SDK in the server side, you should use [@featbit/node-server-sdk](https://github.com/featbit/featbit-node-server-sdk).
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
## Getting started
|
|
22
|
+
### Install
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
npm install @featbit/react-client-sdk
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Prerequisite
|
|
29
|
+
|
|
30
|
+
Before using the SDK, you need to obtain the environment secret and SDK URLs.
|
|
31
|
+
|
|
32
|
+
Follow the documentation below to retrieve these values
|
|
33
|
+
|
|
34
|
+
- [How to get the environment secret](https://docs.featbit.co/sdk/faq#how-to-get-the-environment-secret)
|
|
35
|
+
- [How to get the SDK URLs](https://docs.featbit.co/sdk/faq#how-to-get-the-sdk-urls)
|
|
36
|
+
|
|
37
|
+
### Quick Start
|
|
38
|
+
|
|
39
|
+
The following code demonstrates:
|
|
40
|
+
1. Initialize the SDK
|
|
41
|
+
2. Evaluate flag with userFlags hook
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
import { createRoot } from 'react-dom/client';
|
|
45
|
+
import { asyncWithFbProvider, useFlags, FbProvider } from '@featbit/react-client-sdk';
|
|
46
|
+
|
|
47
|
+
function Game() {
|
|
48
|
+
return (
|
|
49
|
+
<div>
|
|
50
|
+
This is a game
|
|
51
|
+
</div>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function APP() {
|
|
56
|
+
const { flags } = useFlags();
|
|
57
|
+
|
|
58
|
+
const gameEnabled = flags['game-enabled'];
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div>
|
|
62
|
+
<div>{ gameEnabled ? <Game /> : <div>The game is not enabled</div>}</div>
|
|
63
|
+
</div>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
(async () => {
|
|
68
|
+
const configWithAnonymousUser = {
|
|
69
|
+
options: {
|
|
70
|
+
user: {
|
|
71
|
+
name: 'the user name',
|
|
72
|
+
keyId: 'fb-demo-user-key',
|
|
73
|
+
customizedProperties: []
|
|
74
|
+
},
|
|
75
|
+
sdkKey: 'YOUR ENVIRONMENT SECRET',
|
|
76
|
+
streamingUrl: 'THE STREAMING URL',
|
|
77
|
+
eventsUrl: 'THE EVENTS URL'
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const root = createRoot(document.getElementById('root'));
|
|
82
|
+
const Provider = await asyncWithFbProvider(configWithAnonymousUser);
|
|
83
|
+
root.render(
|
|
84
|
+
<Provider>
|
|
85
|
+
<Game />,
|
|
86
|
+
</Provider>
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
// Use the following code in React < 18.0.0
|
|
90
|
+
// ReactDOM.render(
|
|
91
|
+
// <FbProvider>
|
|
92
|
+
// <APP/>
|
|
93
|
+
// </FbProvider>,
|
|
94
|
+
// document.getElementById('root')
|
|
95
|
+
// );
|
|
96
|
+
})();
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Examples
|
|
101
|
+
- [React APP](./examples/react-app)
|
|
102
|
+
- [Next.js APP](./examples/nextjs-app)
|
|
103
|
+
|
|
104
|
+
## SDK
|
|
105
|
+
|
|
106
|
+
### Initializing the SDK
|
|
107
|
+
After you install the dependency, initialize the React SDK. You can do this in one of two ways:
|
|
108
|
+
|
|
109
|
+
- Using the `asyncWithFbProvider` function
|
|
110
|
+
- Using the `withFbProvider` function
|
|
111
|
+
|
|
112
|
+
Both rely on React's Context API which lets you access your flags from any level of your component hierarchy.
|
|
113
|
+
|
|
114
|
+
#### Initializing using asyncWithFbProvider
|
|
115
|
+
|
|
116
|
+
The `asyncWithFbProvider` function initializes the React SDK and returns a provider which is a React component. It is an async function. It accepts a `ProviderConfig` object.
|
|
117
|
+
|
|
118
|
+
`asyncWithFbProvider` cannot be deferred. You must initialize `asyncWithFbProvider` at the app entry point prior to rendering to ensure flags and the client are ready at the beginning of the app.
|
|
119
|
+
|
|
120
|
+
```javascript
|
|
121
|
+
import { createRoot } from 'react-dom/client';
|
|
122
|
+
import { asyncWithFbProvider } from '@featbit/react-client-sdk';
|
|
123
|
+
|
|
124
|
+
(async () => {
|
|
125
|
+
const config = {
|
|
126
|
+
options: {
|
|
127
|
+
sdkKey: 'YOUR ENVIRONMENT SECRET',
|
|
128
|
+
streamingUrl: 'THE STREAMING URL',
|
|
129
|
+
eventsUrl: 'THE EVENTS URL',
|
|
130
|
+
user: {
|
|
131
|
+
userName: 'USER NAME',
|
|
132
|
+
id: 'USER ID',
|
|
133
|
+
customizedProperties: [
|
|
134
|
+
{
|
|
135
|
+
"name": "age",
|
|
136
|
+
"value": '18'
|
|
137
|
+
}
|
|
138
|
+
]
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const root = createRoot(document.getElementById('root'));
|
|
144
|
+
const FbProvider = await asyncWithFbProvider(config);
|
|
145
|
+
root.render(
|
|
146
|
+
<FbProvider>
|
|
147
|
+
<YourApp />,
|
|
148
|
+
</FbProvider>
|
|
149
|
+
);
|
|
150
|
+
})();
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
After initialization is complete, your flags and the client become available at the start of your React app lifecycle. This ensures your app does not flicker due to flag changes at startup time.
|
|
155
|
+
|
|
156
|
+
> Rendering may be delayed
|
|
157
|
+
Because the asyncWithFbProvider function is asynchronous, the rendering of your React app is delayed until initialization is completed. This can take up to 100 milliseconds, but often completes sooner. Alternatively, you can use the withFbProvider function if you prefer to render your app first and then process flag updates after rendering is complete.
|
|
158
|
+
|
|
159
|
+
> This function requires React 16.8.0 or later
|
|
160
|
+
The asyncWithFbProvider function uses the Hooks API, which requires React version 16.8.0 or later.
|
|
161
|
+
|
|
162
|
+
#### Initializing using withFbProvider
|
|
163
|
+
|
|
164
|
+
The `withFbProvider` function initializes the React SDK and wraps your root component in a **Context.Provider**. It accepts a `ProviderConfig` object used to configure the React SDK.
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
import { withFbProvider } from '@featbit/react-client-sdk';
|
|
168
|
+
|
|
169
|
+
const config = {
|
|
170
|
+
options: {
|
|
171
|
+
sdkKey: 'YOUR ENVIRONMENT SECRET',
|
|
172
|
+
streamingUrl: 'THE STREAMING URL',
|
|
173
|
+
eventsUrl: 'THE EVENTS URL',
|
|
174
|
+
user: {
|
|
175
|
+
userName: 'USER NAME',
|
|
176
|
+
id: 'USER ID',
|
|
177
|
+
customizedProperties: [
|
|
178
|
+
{
|
|
179
|
+
"name": "age",
|
|
180
|
+
"value": '18'
|
|
181
|
+
}
|
|
182
|
+
]
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export default withFbProvider(config)(YourApp);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### Subscribing to flag changes
|
|
191
|
+
|
|
192
|
+
The React SDK automatically subscribes to flag change events. This is different from the JavaScript SDK, where customers need to opt in to event listening.
|
|
193
|
+
|
|
194
|
+
### Configuring the React SDK
|
|
195
|
+
|
|
196
|
+
The `ProviderConfig` object provides configuration to both `withFbProvider` and `asyncWithFbProvider` function.
|
|
197
|
+
|
|
198
|
+
The only mandatory property is the **options**, it is the config needed to initialize the `@featbit/js-client-sdk`. To know more details about the **options**, please refer to [@featbit/js-client-sdk](https://github.com/featbit/featbit-js-client-sdk). All other properties are React SDK related.
|
|
199
|
+
|
|
200
|
+
The complete liste of the available properties:
|
|
201
|
+
|
|
202
|
+
- **options**: the initialization config for `@featbit/js-client-sdk`. **mandatory**
|
|
203
|
+
|
|
204
|
+
You can use the **[options.bootstrap](https://github.com/featbit/featbit-js-client-sdk?tab=readme-ov-file#bootstrap)** option to populate the SDK with default values.
|
|
205
|
+
- **reactOptions**: You can use this option to enable automatic camel casing of flag keys when using the React SDK. the default value is false **not mandatory**
|
|
206
|
+
- **deferInitialization**: This property allows SDK initialization to be deferred until you define the fbClient property. the default value is false **not mandatory**
|
|
207
|
+
|
|
208
|
+
asyncWithFbProvider does not support deferInitialization. You must initialize asyncWithFbProvider at the app entry point prior to rendering to ensure flags and the client are ready at the beginning of your app.
|
|
209
|
+
|
|
210
|
+
By deferring SDK initialization, you defer all steps which take place as part of SDK initialization, including reading flag values from local storage and sending the SDK's ready event.
|
|
211
|
+
|
|
212
|
+
The one exception to this rule is that the SDK continues to load bootstrapped flag values as long as the bootstrapped values are provided as a map of flag keys and values. If you indicate that the SDK should bootstrap flags from local storage, this will not happen until the SDK initializes.
|
|
213
|
+
|
|
214
|
+
The following is an example ProviderConfig object including each of the above properties:
|
|
215
|
+
```javascript
|
|
216
|
+
{
|
|
217
|
+
options: {
|
|
218
|
+
sdkKey: 'YOUR ENVIRONMENT SECRET',
|
|
219
|
+
streamingUrl: 'THE STREAMING URL',
|
|
220
|
+
eventsUrl: 'THE EVENTS URL',
|
|
221
|
+
user: {
|
|
222
|
+
userName: 'USER NAME',
|
|
223
|
+
id: 'USER ID',
|
|
224
|
+
customizedProperties: [
|
|
225
|
+
{
|
|
226
|
+
"name": "age",
|
|
227
|
+
"value": '18'
|
|
228
|
+
}
|
|
229
|
+
]
|
|
230
|
+
}
|
|
231
|
+
...
|
|
232
|
+
},
|
|
233
|
+
reactOptions: {
|
|
234
|
+
useCamelCaseFlagKeys: false
|
|
235
|
+
},
|
|
236
|
+
deferInitialization: false
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Consuming flags
|
|
242
|
+
|
|
243
|
+
#### Consuming flags in class component
|
|
244
|
+
|
|
245
|
+
There are two ways to consume the flags.
|
|
246
|
+
|
|
247
|
+
##### Using contextType property
|
|
248
|
+
```javascript
|
|
249
|
+
import { context } from '@featbit/react-client-sdk';
|
|
250
|
+
|
|
251
|
+
class MyComponent extends React.Component {
|
|
252
|
+
static contextType = context;
|
|
253
|
+
|
|
254
|
+
constructor(props) {
|
|
255
|
+
super(props);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
render() {
|
|
259
|
+
const { flags } = this.context;
|
|
260
|
+
|
|
261
|
+
return (
|
|
262
|
+
<div>{ flags['dev-test-flag'] ? 'Flag on' : 'Flag off' }</div>
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export default MyComponent;
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
##### Using withFbConsumer
|
|
271
|
+
|
|
272
|
+
The return value of withFbConsumer is a wrapper function that takes your component and returns a React component injected with flags & fbClient as props.
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
import { withFbConsumer } from '@featbit/react-client-sdk';
|
|
276
|
+
|
|
277
|
+
class MyComponent extends React.Component {
|
|
278
|
+
render() {
|
|
279
|
+
const { flags } = this.props;
|
|
280
|
+
|
|
281
|
+
return (
|
|
282
|
+
<div>
|
|
283
|
+
<div>{flags.flag1}</div>
|
|
284
|
+
</div>
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export default withFbConsumer()(Board)
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
#### Consuming flags in function component
|
|
293
|
+
There are two ways to consume the flags.
|
|
294
|
+
|
|
295
|
+
##### Using withFbConsumer
|
|
296
|
+
The return value of withFbConsumer is a wrapper function that takes your component and returns a React component injected with flags & fbClient as props.
|
|
297
|
+
|
|
298
|
+
```javascript
|
|
299
|
+
import { withFbConsumer } from '@featbit/react-client-sdk';
|
|
300
|
+
|
|
301
|
+
const Home = ({ flags, fbClient /*, ...otherProps */ }) => {
|
|
302
|
+
// You can call any of the methods from the JavaScript SDK
|
|
303
|
+
// fbClient.identify({...})
|
|
304
|
+
|
|
305
|
+
return flags['dev-test-flag'] ? <div>Flag on</div> : <div>Flag off</div>;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
export default withFbConsumer()(Home);
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
##### Using Hooks
|
|
312
|
+
|
|
313
|
+
The React SDK offers two custom hooks which you can use as an alternative to `withFbConsumer`:
|
|
314
|
+
- `useFlags`
|
|
315
|
+
- `useFbClient`
|
|
316
|
+
|
|
317
|
+
> These functions require React 16.8.0 or later
|
|
318
|
+
useFlags and useFbClient use the Hooks API, which requires React version 16.8.0 or later.
|
|
319
|
+
|
|
320
|
+
useFlags is a custom hook which returns all feature flags. It uses the useContext primitive to access the FeatBit context set up by asyncWithFbProvider or withFbProvider. You still must use the asyncWithFbProvider or the withFbProvider higher-order component at the root of your application to initialize the React SDK and populate the context with fbClient and your flags.
|
|
321
|
+
|
|
322
|
+
useFbClient is the second custom hook which returns the underlying FeatBit's JavaScript SDK client object. Like the useFlags custom hook, useFbClient also uses the useContext primitive to access the context set up by asyncWithFbProvider or withFbProvider. You still must use the asyncWithFbProvider or the withFbProvider higher-order component to initialize the React SDK to use this custom hook.
|
|
323
|
+
|
|
324
|
+
Here is an example of how to use those two hooks:
|
|
325
|
+
|
|
326
|
+
```javascript
|
|
327
|
+
import { useFlags, useFbClient } from '@featbit/react-client-sdk';
|
|
328
|
+
|
|
329
|
+
const MyComponent = props => {
|
|
330
|
+
const fbClient = useFbClient();
|
|
331
|
+
|
|
332
|
+
const { flag1, flag2 } = useFlags();
|
|
333
|
+
// or use
|
|
334
|
+
const flags = useFlags();
|
|
335
|
+
// then use flags.flag1 or flags['flag1'] to reference the flag1 feature flag
|
|
336
|
+
|
|
337
|
+
return (
|
|
338
|
+
<div>
|
|
339
|
+
<div>1: {flag1}</div>
|
|
340
|
+
<div>2: {flag2}</div>
|
|
341
|
+
</div>
|
|
342
|
+
);
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
export default MyComponent;
|
|
347
|
+
|
|
348
|
+
```
|
|
349
|
+
### Switch user after initialization
|
|
350
|
+
|
|
351
|
+
If the user is not available during the initialization, you can call the **identity()** method on **fbClient** to set the user after initialization.
|
|
352
|
+
|
|
353
|
+
#### Switch user in class component
|
|
354
|
+
|
|
355
|
+
```javascript
|
|
356
|
+
import { withFbConsumer, useFbClient } from '@featbit/react-client-sdk';
|
|
357
|
+
|
|
358
|
+
class LoginComponent extends React.Component {
|
|
359
|
+
|
|
360
|
+
handleLogin = async () => {
|
|
361
|
+
const { fbClient } = this.props;
|
|
362
|
+
const user = {}; // use your user object
|
|
363
|
+
await fbClient.identify(user);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
render() {
|
|
367
|
+
return (
|
|
368
|
+
<div>
|
|
369
|
+
<button onClick={this.handleLogin}>Login</button>
|
|
370
|
+
</div>
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export default withFbConsumer()(LoginComponent)
|
|
376
|
+
````
|
|
377
|
+
|
|
378
|
+
#### Switch user in function component
|
|
379
|
+
|
|
380
|
+
```javascript
|
|
381
|
+
import { useFbClient } from '@featbit/react-client-sdk';
|
|
382
|
+
|
|
383
|
+
const LoginComponent = () => {
|
|
384
|
+
const fbClient = useFbClient();
|
|
385
|
+
|
|
386
|
+
handleLogin = async () => {
|
|
387
|
+
const user = {}; // use your user object
|
|
388
|
+
await fbClient.identify(user);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return (
|
|
392
|
+
<div>
|
|
393
|
+
<button onClick={handleLogin}>Login</button>
|
|
394
|
+
</div>
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
````
|
|
399
|
+
|
|
400
|
+
### Populating the SDK with fallback flag values
|
|
401
|
+
|
|
402
|
+
As mentioned above, you can use the **options.bootstrap** option to populate the SDK with default values for your flags. This option is useful when you want to provide default values for your flags before the SDK initializes.
|
|
403
|
+
If a flag is not available from the SDK, the SDK uses the default value you provide in the bootstrap object.
|
|
404
|
+
|
|
405
|
+
```javascript
|
|
406
|
+
options: {
|
|
407
|
+
bootstrap: [
|
|
408
|
+
{
|
|
409
|
+
"id": "my-flag",
|
|
410
|
+
"variation": "on"
|
|
411
|
+
},
|
|
412
|
+
{
|
|
413
|
+
"id": "second-flag",
|
|
414
|
+
"variation": "AlphaGo"
|
|
415
|
+
}
|
|
416
|
+
]
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
## Flag keys
|
|
421
|
+
FeatBit primarily identifies feature flags by a key which must contain only alphanumeric characters, dots (.), underscores (_), and dashes (-). These keys are used across all of our APIs as well as in the SDKs to identify flags.
|
|
422
|
+
|
|
423
|
+
However, JavaScript and React cannot access keys with a dot notation, so the React SDK can change all flag keys to camel case (you need to activate this with the **reactOptions.useCamelCaseFlagKeys** parameter). A flag with key `dev-flag-test` is accessible as `flags.devFlagTest`. This notation **flags['dev-flag-test']** should be used if useCamelCaseFlagKeys is disabled, which is by default.
|
|
424
|
+
|
|
425
|
+
Be aware, by activating useCamelCaseFlagKeys, you would see following problems:
|
|
426
|
+
|
|
427
|
+
- It is possible to induce a key collision if there are multiple flag keys which resolve to the same camel-case key. For example, `dev-flag-test` and `dev.flag.test` are unique keys, but the React SDK changes them to the same camel-case key.
|
|
428
|
+
- If a flag key contains three or more capital letters in a row, the SDK automatically converts all letters between the first and last capital letter to lower case. For example, the SDK converts a flag with the key `devQAFlagTest` to `devQaFlagTest`. If you use devQAFlagTest with the useFlags() hook, the SDK does not find the flag.
|
|
429
|
+
- Because the camel-case functionality is implemented in the React SDK instead of in the underlying JavaScript SDK, the underlying client object and functionality provided by the JavaScript SDK reflect flag keys in their original format. Only React-specific contexts such as your injected props use camel case.
|
|
430
|
+
|
|
431
|
+
> If you've enabled the useCamelCaseFlagKeys option to true, attempting to access a flag using its original key will trigger a warning message in the console:
|
|
432
|
+
> **You're attempting to access a flag with its original keyId: xxx, even though useCamelCaseFlagKeys is set to true.**
|
|
433
|
+
## Importing types
|
|
434
|
+
|
|
435
|
+
In addition to its own bundled types, the React SDK uses types from `@featbit/js-client-sdk`, these types are re-exported by the React SDK. You can import these types directly from the React SDK
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { ProviderConfig } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* This is an async function which initializes feature-flags.co's JS SDK (`@featbit/js-client-sdk`)
|
|
5
|
+
* and awaits it so all flags and the fbClient are ready before the consumer app is rendered.
|
|
6
|
+
*
|
|
7
|
+
* The difference between `withFbProvider` and `asyncWithFbProvider` is that `withFbProvider` initializes
|
|
8
|
+
* `@featbit/js-client-sdk` at componentDidMount. This means your flags and the fbClient are only available after
|
|
9
|
+
* your app has mounted. This can result in a flicker due to flag changes at startup time.
|
|
10
|
+
*
|
|
11
|
+
* `asyncWithFbProvider` initializes `@featbit/js-client-sdk` at the entry point of your app prior to render.
|
|
12
|
+
* This means that your flags and the fbClient are ready at the beginning of your app. This ensures your app does not
|
|
13
|
+
* flicker due to flag changes at startup time.
|
|
14
|
+
*
|
|
15
|
+
* `asyncWithFbProvider` accepts a config object which is used to initialize `@featbit/js-client-sdk`.
|
|
16
|
+
*
|
|
17
|
+
* `asyncWithFbProvider` does not support the `deferInitialization` config option because `asyncWithFbProvider` needs
|
|
18
|
+
* to be initialized at the entry point prior to render to ensure your flags and the fbClient are ready at the beginning
|
|
19
|
+
* of your app.
|
|
20
|
+
*
|
|
21
|
+
* It returns a provider which is a React FunctionComponent which:
|
|
22
|
+
* - saves all flags and the ldClient instance in the context API
|
|
23
|
+
* - subscribes to flag changes and propagate them through the context API
|
|
24
|
+
*
|
|
25
|
+
* @param config - The configuration used to initialize FeatBit's JS SDK
|
|
26
|
+
*/
|
|
27
|
+
export default function asyncWithFbProvider(config: ProviderConfig): Promise<({ children }: {
|
|
28
|
+
children: ReactNode;
|
|
29
|
+
}) => React.JSX.Element>;
|