[React] Broadcaster + Operator + Listener pattern -- 19. useBroadcaster & useListener

Instead of always combining useState and useEffect, when can create a utility useBroadcaster hook which allows us to pass in a broadcaster.

export let useBroadcaster = (broadcaster, deps = []) => {
  let [state, setState] = useState(null)
  useEffect(() => {
    broadcaster(setState)
  }, deps)
  return state
}

The important thing to notice here is 'useEffect' only run once in the very begining. Its job is to pass 'setState' function to listener. That's why in the later code you will see we need to check the 'value' come in is actually a function or not:

if (typeof value === "function") {
    listener = value
    return
  }

 

 

We need to be able to capture user input from React and use them as a broadcaster, so we'll lean on the useCallback hook and convert it into a broadcaster. We'll take what we've learned from using callbacks and closures to set up a pattern that can capture a setState then pass values to setState.

复制代码
let listener
let callbackListener = (value) => {
  if (typeof value === "function") {
    listener = value
    return
  }
  listener(value)
}

let App = () => {
  let onInput = useCallback(callbackListener)
  let state = useBroadcaster(targetValue(onInput))
  return (
    <div>
      <input type="text" onInput={onInput} />
      <p>{state}</p>
    </div>
  )
}
复制代码

 

We can capture the pattern we created with useCallback into our own useListener hook. 

  • This is another example of how you can encapsulates and wrap a piece of functionallity to make it shareable throw out your application code.
复制代码
// Before
let listener
let callbackListener = (value) => {
  if (typeof value === "function") {
    listener = value
    return
  }
  listener(value)
}

let App = () => {
  let onInput = useCallback(callbackListener)
  let state = useBroadcaster(targetValue(onInput))
  return (
    <div>
      <input type="text" onInput={onInput} />
      <p>{state}</p>
    </div>
  )
}

// After:
export let useListener = (deps = []) => {
  let listener // Now this value is not attatch to the global scope of your module, but to this hook!
  let callbackListener = (value) => {
    // same here with this function
    if (typeof value === "function") {
      listener = value
      return
    }
    listener(value)
  }
  return useCallback(callbackListener, deps)
}

let App = () => {
  let onInput = useListener()
  let state = useBroadcaster(targetValue(onInput))

  return (
    <div>
      <input type="text" onInput={onInput} />
      <p>{state}</p>
    </div>
  )
}
复制代码

 

The pattern is more important here.

What we want is

  • we can pass a 'broadcaster' to 'useBroadcaster' hook
  • we can pass a 'listener' to 'useListener' hook
  • the working flow can hook up with React framework

 

Imporve for 'useBroadcaster':

复制代码
export let useBroadcaster = (broadcaster, deps = []) => {
  let [state, setState] = useState(null)
  useEffect(() => {
    broadcaster((value) => {
      if (value === done) {
        return
      }
      setState(value)
    })
  }, deps)
  return state
}
复制代码

 

posted @   Zhentiw  阅读(125)  评论(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工具
历史上的今天:
2017-11-17 [ES6] Symbol
2016-11-17 [Angular 2] Set Values on Generated Angular 2 Templates with Template Context
2016-11-17 [Angular2 Router] Resolving route data in Angular 2
2014-11-17 [ES6] 02. Traceur compiler and Grunt
2014-11-17 [Node.js] Level 6. Socket.io
2014-11-17 [Grunt] Cleaning your build folder with grunt-contrib-clean
2014-11-17 [Grunt] Minifying your output with grunt-uglify
点击右上角即可分享
微信分享提示