英文:
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:
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
(async () => {
const LDProvider = await asyncWithLDProvider({
clientSideID: 'CLIENT_SIDE_ID',
context: {
"kind": "user",
"key": "KEY",
"name": "NAME",
"email": "abc@example.com"
},
});
render(
<LDProvider>
<ControllerPage />
</LDProvider>,
document.getElementById('reactDiv'),
);
})();
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:
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
(async () => {
const LDProvider = await asyncWithLDProvider({
clientSideID: 'CLIENT_SIDE_ID',
context: {
"kind": "user",
"key": "KEY",
"name": "NAME",
"email": "abc@example.com"
},
});
render(
<LDProvider>
<ControllerPage />
</LDProvider>,
document.getElementById('reactDiv'),
);
})();
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 <LDProvider></LDProvider>
.
So i am asking is there any solution for this problem that I can use launchdarkly implementation using function without Wrapping my code with <LDProvider></LDProvider>
.
答案1
得分: 1
以下是您要翻译的内容:
NPM包:
npm install launchdarkly-js-client-sdk
代码:
import * as LDClient from 'launchdarkly-js-client-sdk';
const randomString = Math.random().toString(36).slice(2);
const newUser = {
"kind": "user",
"key": `guest-user-${randomString}`,
"name": `guest-user-${randomString}`,
"email": `guest-user-${randomString}@email.com`
};
const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
ldclient.on("ready", () => {
const flagData = ldclient.allFlags();
const flagResponse = flagData['feature_name'];
if (flagResponse != undefined && flagResponse) {
// Do this
} else {
// Do this
}
});
上述代码用于评估所有标志。如果您想要评估特定的标志,那么代码如下:
import * as LDClient from 'launchdarkly-js-client-sdk';
const randomString = Math.random().toString(36).slice(2);
const newUser = {
"kind": "user",
"key": `guest-user-${randomString}`,
"name": `guest-user-${randomString}`,
"email": `guest-user-${randomString}@email.com`
};
const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
ldclient.on("ready", () => {
const flagResponse = ldclient.variation('flag-key-123abc', false);
if (flagResponse != undefined && flagResponse) {
// Do this
} else {
// Do this
}
});
上述代码中,false代表如果出现错误,应该使用的默认值。
英文:
This can be easily done as :
NPM Package :
npm install launchdarkly-js-client-sdk
Code :
import * as LDClient from 'launchdarkly-js-client-sdk';
const randomString = Math.random().toString(36).slice(2)
const newUser = {
"kind": "user",
"key": `guest-user-${randomString}`,
"name": `guest-user-${randomString}`,
"email": `guest-user-${randomString}@email.com`
}
const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
ldclient.on("ready", () => {
const flagData = ldclient.allFlags();
const flagResponse = flagData['feature_name']
if (flagResponse != undefined && flagResponse) {
// Do this
}else{
// Do this
}
Above code is used to evaluates all the flags. If you want to evaluate a particular flag then it would be like this :
import * as LDClient from 'launchdarkly-js-client-sdk';
const randomString = Math.random().toString(36).slice(2)
const newUser = {
"kind": "user",
"key": `guest-user-${randomString}`,
"name": `guest-user-${randomString}`,
"email": `guest-user-${randomString}@email.com`
}
const ldclient = await LDClient.initialize('LAUNCHDARKLY_ID', newUser);
ldclient.on("ready", () => {
const flagResponse = ldclient.variation('flag-key-123abc',false);
if (flagResponse != undefined && flagResponse) {
// Do this
}else{
// Do this
}
Above , false represents if somehow error occurred then what would be the default value one should use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论