实现Launchdarkly在React中的功能,无需包装在组件中。

huangapple go评论92阅读模式
英文:

Implement Launchdarkly with function in react without wrapping in Component

问题

I am currently following the guidelines provided by launchdarkly, and as per documentation, I used the following code:

  1. import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
  2. (async () => {
  3. const LDProvider = await asyncWithLDProvider({
  4. clientSideID: 'CLIENT_SIDE_ID',
  5. context: {
  6. "kind": "user",
  7. "key": "KEY",
  8. "name": "NAME",
  9. "email": "abc@example.com"
  10. },
  11. });
  12. render(
  13. <LDProvider>
  14. <ControllerPage />
  15. </LDProvider>,
  16. document.getElementById('reactDiv'),
  17. );
  18. })();

This somehow creates a problem as it's not working properly.

Also, this is not my requirement. Even if it works, I don't want to wrap my code with <LDProvider></LDProvider>. So, I am asking if there is any solution for this problem that I can use launchdarkly implementation using a function without wrapping my code with <LDProvider></LDProvider>.

英文:

Currently I am following the guidelines provided by launchdarkly and as per documentation I used:

  1. import { asyncWithLDProvider } from &#39;launchdarkly-react-client-sdk&#39;;
  2. (async () =&gt; {
  3. const LDProvider = await asyncWithLDProvider({
  4. clientSideID: &#39;CLIENT_SIDE_ID&#39;,
  5. context: {
  6. &quot;kind&quot;: &quot;user&quot;,
  7. &quot;key&quot;: &quot;KEY&quot;,
  8. &quot;name&quot;: &quot;NAME&quot;,
  9. &quot;email&quot;: &quot;abc@example.com&quot;
  10. },
  11. });
  12. render(
  13. &lt;LDProvider&gt;
  14. &lt;ControllerPage /&gt;
  15. &lt;/LDProvider&gt;,
  16. document.getElementById(&#39;reactDiv&#39;),
  17. );
  18. })();

This somehow create problem as it's not working properly .

Also this is not my requirement even if it work I don't want to wrap my read code with &lt;LDProvider&gt;&lt;/LDProvider&gt;.
So i am asking is there any solution for this problem that I can use launchdarkly implementation using function without Wrapping my code with &lt;LDProvider&gt;&lt;/LDProvider&gt; .

答案1

得分: 1

以下是您要翻译的内容:

NPM包:

npm install launchdarkly-js-client-sdk

代码:

  1. import * as LDClient from 'launchdarkly-js-client-sdk';
  2. const randomString = Math.random().toString(36).slice(2);
  3. const newUser = {
  4. "kind": "user",
  5. "key": `guest-user-${randomString}`,
  6. "name": `guest-user-${randomString}`,
  7. "email": `guest-user-${randomString}@email.com`
  8. };
  9. const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
  10. ldclient.on("ready", () => {
  11. const flagData = ldclient.allFlags();
  12. const flagResponse = flagData['feature_name'];
  13. if (flagResponse != undefined && flagResponse) {
  14. // Do this
  15. } else {
  16. // Do this
  17. }
  18. });

上述代码用于评估所有标志。如果您想要评估特定的标志,那么代码如下:

  1. import * as LDClient from 'launchdarkly-js-client-sdk';
  2. const randomString = Math.random().toString(36).slice(2);
  3. const newUser = {
  4. "kind": "user",
  5. "key": `guest-user-${randomString}`,
  6. "name": `guest-user-${randomString}`,
  7. "email": `guest-user-${randomString}@email.com`
  8. };
  9. const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
  10. ldclient.on("ready", () => {
  11. const flagResponse = ldclient.variation('flag-key-123abc', false);
  12. if (flagResponse != undefined && flagResponse) {
  13. // Do this
  14. } else {
  15. // Do this
  16. }
  17. });

上述代码中,false代表如果出现错误,应该使用的默认值。

英文:

This can be easily done as :

NPM Package :

  1. npm install launchdarkly-js-client-sdk

Code :

  1. import * as LDClient from &#39;launchdarkly-js-client-sdk&#39;;
  2. const randomString = Math.random().toString(36).slice(2)
  3. const newUser = {
  4. &quot;kind&quot;: &quot;user&quot;,
  5. &quot;key&quot;: `guest-user-${randomString}`,
  6. &quot;name&quot;: `guest-user-${randomString}`,
  7. &quot;email&quot;: `guest-user-${randomString}@email.com`
  8. }
  9. const ldclient = await LDClient.initialize(&#39;LAUNCHDARKLY_ID&#39;, newUser);
  10. ldclient.on(&quot;ready&quot;, () =&gt; {
  11. const flagData = ldclient.allFlags();
  12. const flagResponse = flagData[&#39;feature_name&#39;]
  13. if (flagResponse != undefined &amp;&amp; flagResponse) {
  14. // Do this
  15. }else{
  16. // Do this
  17. }

Above code is used to evaluates all the flags. If you want to evaluate a particular flag then it would be like this :

  1. import * as LDClient from &#39;launchdarkly-js-client-sdk&#39;;
  2. const randomString = Math.random().toString(36).slice(2)
  3. const newUser = {
  4. &quot;kind&quot;: &quot;user&quot;,
  5. &quot;key&quot;: `guest-user-${randomString}`,
  6. &quot;name&quot;: `guest-user-${randomString}`,
  7. &quot;email&quot;: `guest-user-${randomString}@email.com`
  8. }
  9. const ldclient = await LDClient.initialize(&#39;LAUNCHDARKLY_ID&#39;, newUser);
  10. ldclient.on(&quot;ready&quot;, () =&gt; {
  11. const flagResponse = ldclient.variation(&#39;flag-key-123abc&#39;,false);
  12. if (flagResponse != undefined &amp;&amp; flagResponse) {
  13. // Do this
  14. }else{
  15. // Do this
  16. }

Above , false represents if somehow error occurred then what would be the default value one should use.

huangapple
  • 本文由 发表于 2023年5月14日 18:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定