自定义钩子内useEffect的生命周期是什么?

时间:2019-12-17 08:37:39

标签: reactjs

我知道useEffect就像组件的事件侦听器,该组件在每次重新渲染时运行(忽略dep)。

当它在自定义钩子中还是自定义钩子执行完后立即销毁,这仍然是正确的吗?下面的示例代码:

export default props => {
  const [data, setDataWithSideEffects] = customHook('initial value');
  return (
    <div>
      <button onClick={e => setDataWithSideEffects(e)>Click</button>
      <div>{data.title}</div>
    </div>
  )
}

const customHook = initialValue => {
  const [data, setData] = useState(initialValue);

  const setDataWithSideEffects = async event => {
    event.preventDefault();
    const response = await fetch("http://someUrl.com/api")
    const json = await response.json()
    setData(json); 
  }

  // how long does this live?
  useEffect(() => { // other side effects done every render });

  return [data, setDataWithSideEffects];
}

0 个答案:

没有答案