React本机组件一经计算就不会更新

发布于 2025-01-17 14:23:18 字数 1189 浏览 0 评论 0原文

我在屏幕中有一个组件列表。每个组件都会对 useEffect 进行计算。我希望他们在完成每次计算后立即更新,但相反,它们会在所有计算完成后全部更新。

我做错了什么?

组件示例:

import React, {useEffect, useState} from 'react';
import {View, Text} from 'react-native';

const Teste = () => {
  const Item = props => {
    const [tempo, setTempo] = useState('calculando...');

    useEffect(() => {
      const inicio = new Date();
      const rdm = Math.random();
      sleep(rdm * 1000 + 2000);
      setTempo(`${new Date() - inicio} millisegundos`);
    }, []);

    const sleep = milliseconds => {
      const date = Date.now();
      let currentDate = null;
      do {
        currentDate = Date.now();
      } while (currentDate - date < milliseconds);
    };

    return (
      <View style={{marginBottom: 20}}>
        <Text>Item {props.nome}</Text>
        <Text>Tempo - {tempo}</Text>
      </View>
    );
  };

  return (
    <View style={{padding: 40}}>
      <Item nome="1" />
      <Item nome="2" />
      <Item nome="3" />
    </View>
  );
};

export default Teste;

I have a list of components in a screen. Each component makes a calculation on useEffect. I expect then to update as soon as they finish each calculation, but instead they all update after all calculations completes.

What I'm doing wrong?

Example of component:

import React, {useEffect, useState} from 'react';
import {View, Text} from 'react-native';

const Teste = () => {
  const Item = props => {
    const [tempo, setTempo] = useState('calculando...');

    useEffect(() => {
      const inicio = new Date();
      const rdm = Math.random();
      sleep(rdm * 1000 + 2000);
      setTempo(`${new Date() - inicio} millisegundos`);
    }, []);

    const sleep = milliseconds => {
      const date = Date.now();
      let currentDate = null;
      do {
        currentDate = Date.now();
      } while (currentDate - date < milliseconds);
    };

    return (
      <View style={{marginBottom: 20}}>
        <Text>Item {props.nome}</Text>
        <Text>Tempo - {tempo}</Text>
      </View>
    );
  };

  return (
    <View style={{padding: 40}}>
      <Item nome="1" />
      <Item nome="2" />
      <Item nome="3" />
    </View>
  );
};

export default Teste;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱贱哒 2025-01-24 14:23:18

这里有两个问题...

  1. 睡眠功能,我什至无法
  2. 定义item teste> teste组件,这意味着每当teste渲染器时,item重新定义,

我建议将iteg> item移出teste ,并使用基于承诺的 Sleep

const sleep = (ms) => {
  let timer;
  const promise = new Promise(r => {
    timer = setTimeout(r, ms);
  });
  return [ promise, () => { clearTimeout(timer); } ];
};

const Item = ({ nome }) => {
  const [tempo, setTempo] = useState('calculando...');

  useEffect(() => {
    const inicio = new Date();
    const rdm = Math.random();
    const [ delay, cancel ] = sleep(rdm * 1000 + 2000);
    delay.then(() => {
      setTempo(`${new Date() - inicio} millisegundos`);
    });

    return cancel; // make sure timeout is cleared on unmount
  }, []);

  return (
    <View style={{marginBottom: 20}}>
      <Text>Item {nome}</Text>
      <Text>Tempo - {tempo}</Text>
    </View>
  );
};

const Teste = () => (
  <View style={{padding: 40}}>
    <Item nome="1" />
    <Item nome="2" />
    <Item nome="3" />
  </View>
);

在这里 sleep> sleep()返回两者,一旦超时到期,也可以解决一个诺言,以及如果您的组件已卸下,则可以取消延迟的函数。

Two problems here...

  1. That sleep function, I literally can't even
  2. You've defined your Item component inside your Teste component which means whenever Teste renders, Item is redefined

I suggest moving Item out of Teste and using a promise-based sleep

const sleep = (ms) => {
  let timer;
  const promise = new Promise(r => {
    timer = setTimeout(r, ms);
  });
  return [ promise, () => { clearTimeout(timer); } ];
};

const Item = ({ nome }) => {
  const [tempo, setTempo] = useState('calculando...');

  useEffect(() => {
    const inicio = new Date();
    const rdm = Math.random();
    const [ delay, cancel ] = sleep(rdm * 1000 + 2000);
    delay.then(() => {
      setTempo(`${new Date() - inicio} millisegundos`);
    });

    return cancel; // make sure timeout is cleared on unmount
  }, []);

  return (
    <View style={{marginBottom: 20}}>
      <Text>Item {nome}</Text>
      <Text>Tempo - {tempo}</Text>
    </View>
  );
};

const Teste = () => (
  <View style={{padding: 40}}>
    <Item nome="1" />
    <Item nome="2" />
    <Item nome="3" />
  </View>
);

Here sleep() returns both a promise that resolves once the timeout has expired as well as a function to cancel the delay if your component is unmounted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文