0

我在启动黑暗仪表板中更改功能标志值,更新的标志数据没有在应用程序中得到反映。我们需要刷新应用程序或在新选项卡中打开以查看更新的结果

4

1 回答 1

0

我最近使用 React 进行了设置,并在黑暗中启动。我们做了两件事来实现这一点:

  1. 订阅启动黑暗客户端的更改on('change')
  2. 使这些更新触发 React 组件重新渲染(prop、state 或 hook 更改将执行此操作,我们这样做的方式是通过 redux)

在此处查看启动黑暗文档

import * as LDClient from 'launchdarkly-js-client-sdk';

...

// where you initialize the sdk 
ldclient = LDClient.initialize(LAUNCH_DARKLY_CLIENT_ID, user);

// set featureFlags in redux when ld client is ready
ldclient.on('ready', () => {
  const flags = ldclient.allFlags();        // get the flags
  dispatch(actions.setFeatureFlags(flags)); // store them in redux
});

// *the part you're asking about*
// on('change') to subscribe to flag changes
ldclient.on('change', () => {
  // you could listen for the specific flag change, but can also just grab all flags whenever there is a change
  const flags = ldclient.allFlags();
  dispatch(actions.setFeatureFlags(flags));
});

从那里,您的组件可以连接到 redux,并且当标志在 redux 中更新时,您的组件将被传递新的标志道具,从而触发重新渲染。

如果您将 redux 替换为将标志 props/state 传递给组件的其他方法,则同样的想法也可以工作。

于 2021-06-15T00:49:29.553 回答