【问题标题】:How to reflect updated data in react application after changing feature flag value in launch darkly在启动时更改功能标志值后如何在反应应用程序中反映更新的数据
【发布时间】:2021-07-14 15:42:13
【问题描述】:

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

【问题讨论】:

    标签: reactjs runtime launchdarkly


    【解决方案1】:

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

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

    暗中看发布会documentation here

    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 传递给组件的其他方法,同样的想法也可以工作。

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 2020-03-05
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      相关资源
      最近更新 更多