【搬砖】React Hooks tip #1

135 阅读1分钟

Stop thinking in terms of the component lifecycle and start thinking in terms of "when should this effect be re-applied".

❌ "Fetch the data when the component mounts and then whenever the uid changes."

✅ "Fetch the data whenever the uid changes"

用“效果何时被需要”的思维替代掉之前的利用组件生命周期的思维。

❌ 在组件mount时以及uid变化时fetch服务端的数据

✅ 统一成uid变化时fetch服务端的数据