从获取而不是静态数据的加载数据无法正常工作

发布于 2025-01-24 13:00:06 字数 868 浏览 0 评论 0原文

我有一些代码试图获取一些数据并将其保存以更新列表数组,但没有对其进行更新。下面的代码是一些硬编码的数组,可以正常工作,但是在第二个我尝试获取数据但没有什么?

任何想知道我出错的地方,我都不会真正掌握这一点。感谢

有效的代码: -

const data = [
  {id: '1', name: 'A'},
  {id: '2', name: 'B'},
  {id: '3', name: 'C'},
  {id: '4', name: 'D'},
  {id: '5', name: 'E'}
];

export default function App() {
const [lists, setLists] = useState(data);

我想用来获取什么:

export default function App() {
  const fetchURL = 'https://www.uberfantasies.com/rn.php'
    
  const [lists, setLists] = useState('');

  const getData = () =>
    fetch(fetchURL)
      .then((res) => res.json());

  useEffect(() => {
    getData().then((lists) => setLists(res.data)  // If I use data it can display the hard coded array //
  }, []);

我在做什么错?

I have a bit of code that is trying to fetch some data and save it to update a list array but is is not updating it. The code below is some hard coded array that works fine but in the second I try to fetch the data but nothing?

Any idea where I am going wrong, I am not really getting the hang of this. Thanks

The code which works :-

const data = [
  {id: '1', name: 'A'},
  {id: '2', name: 'B'},
  {id: '3', name: 'C'},
  {id: '4', name: 'D'},
  {id: '5', name: 'E'}
];

export default function App() {
const [lists, setLists] = useState(data);

And what I am trying to use to fetch :

export default function App() {
  const fetchURL = 'https://www.uberfantasies.com/rn.php'
    
  const [lists, setLists] = useState('');

  const getData = () =>
    fetch(fetchURL)
      .then((res) => res.json());

  useEffect(() => {
    getData().then((lists) => setLists(res.data)  // If I use data it can display the hard coded array //
  }, []);

What am I doing wrong?

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

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

发布评论

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

评论(2

咽泪装欢 2025-01-31 13:00:06

这将加载数据一次并在组件安装时更新列表

const [lists, setLists] = useState('')

useEffect(() => {
  const fetchURL = 'https://www.uberfantasies.com/rn.php'
  fetch(fetchURL)
    .then((res) => res.json())
    .then((json) => setLists(json?.data))
    .catch(console.warn)
}, [])

This will load the data once and update the list when the component mounts

const [lists, setLists] = useState('')

useEffect(() => {
  const fetchURL = 'https://www.uberfantasies.com/rn.php'
  fetch(fetchURL)
    .then((res) => res.json())
    .then((json) => setLists(json?.data))
    .catch(console.warn)
}, [])
幻想少年梦 2025-01-31 13:00:06

您可以记录getData函数的响应,而不是将其设置为状态吗?您还可以将功能重构为这样的东西吗?

另外,在使用效果函数中,该res变量来自哪里?我认为应该是列表

const getData = async () => {
  const res = await fetch(fetchURL);
  console.log(res);
  const json = await res.json();
  console.log(json);
  return json;
};

useEffect(() => {
  getData().then((lists) => console.log(lists));
}, [])

Can you log the response of your getData function instead of setting it to state? Also can you refactor your function to something like this pls.

Also, in the useEffect function, where did that res variable come from? I assume it should be lists.

const getData = async () => {
  const res = await fetch(fetchURL);
  console.log(res);
  const json = await res.json();
  console.log(json);
  return json;
};

useEffect(() => {
  getData().then((lists) => console.log(lists));
}, [])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文