[React] Build a feature toggle devtool

If you find yourself filling out the same form over and over again, or working through a weird workflow to enable certain features in your app just to get things developed, then custom DevTools for your app will enhance your productivity. There are so many things you can do once you are enabled to write arbitrary development-only code in your app. In this lesson we'll see how we can wire that up to make it possible.

 

in App.js, there is a feature toggle to switch the logo:

复制代码
import React from 'react'
import featureToggles from './feature-toggles'
import logo from './logo.svg'
import './App.css'

function App() {
  return (
    <div className="App">
      <header className="App-header">
        {featureToggles.tacos ? (
          <span className="App-logo" role="img" aria-label="taco">
            🌮
          </span>
        ) : (
          <img src={logo} className="App-logo" alt="logo" />
        )}
        <a
          className="App-link"
          href={
            featureToggles.tacos
              ? 'https://en.wikipedia.org/wiki/Taco'
              : 'https://reactjs.org'
          }
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn about {featureToggles.tacos ? ' Tacos' : ' React'}
        </a>
      </header>
    </div>
  )
}

export default App
复制代码

 

dev-tools/

--dev-tools.js

--load.js

--dev-tools.local.js

 

In app's index.js file, we call load.js to load devtool to initial the settings:

复制代码
import loadDevTools from './dev-tools/load'
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import App from './App'

loadDevTools(() => {
  ReactDOM.render(<App />, document.getElementById('root'))
})
复制代码

 

load.js:

复制代码
function load(callback) {
  if (process.env.NODE_ENV === 'development') {
    import('./dev-tools')
      .then(mod => mod.install())
      .finally(callback)
  } else {
    callback()
  }
}

export default load
复制代码

dev-tools.js:  Provide the UI for developer and load local devtool file

复制代码
import React from 'react'
import ReactDOM from 'react-dom'

const requireDevToolsLocal = require.context(
  './',
  false,
  /dev-tools\.local\.js/,
)
const local = requireDevToolsLocal.keys()[0]
if (local) {
  requireDevToolsLocal(local)
}

async function install() {
  function DevTools() {
    return 'Hello from the devtools'
  }

  const devToolsRoot = document.createElement('div')
  document.body.appendChild(devToolsRoot)

  ReactDOM.render(<DevTools />, devToolsRoot)
}

export {install}
复制代码

dev-tools.local.js: modify the settings

import featureToggle from '../feature-toggles'

featureToggle.tacos = true

 

feature-toggles.js

// APP_CONFIG is set via the `config.js` script that's in /public
const featureToggles = window.APP_CONFIG.featureToggles

export default featureToggles

config.js;

window.APP_CONFIG = {
  featureToggles: {},
}

 

Refer

posted @   Zhentiw  阅读(109)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-12-26 [Angular] Dynamic component rendering by using *ngComponentOutlet
2018-12-26 [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
2015-12-26 [Node.js] node-persist: localStorage on the server
2015-12-26 [ES6] for..in && for..of
2015-12-26 [Falcor] Building Paths Programmatically
2014-12-26 [Angular-Scaled Web] 9. Control your promises with $q
2014-12-26 [Angular-Scaled Web] 8. Using $http to load JSON data
点击右上角即可分享
微信分享提示