首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在对jsx的提取调用中共享数据

在对jsx的提取调用中共享数据
EN

Stack Overflow用户
提问于 2022-07-25 21:59:21
回答 2查看 28关注 0票数 0

所以我正在构建一个项目

作为其中的一部分,我执行一个提取调用,并console.log响应。

代码语言:javascript
复制
import React, { useState, useEffect } from 'react';

import Downshift from 'downshift'


export default function TestSearchBar() {
  const [pokemonData, setPokemonData] = useState(null);

  useEffect(() => {
    Pokemons();

    async function Pokemons() {
      const res = await fetch(
        `http://localhost:3001/pokemon/names`
      );
      const data = await res.json();
      setPokemonData(data);
    }

  }, []);
  const items = pokemonData;
  return (
    <div>
      <Downshift
        onChange={selection =>
          fetch(`http://localhost:3001/search/${selection.value}`).then(res => res.json()).then(data => console.log(data))
        }
        itemToString={item => (item ? item.value : '')}
      >
        {({
          getInputProps,
          getItemProps,
          getLabelProps,
          getMenuProps,
          isOpen,
          inputValue,
          highlightedIndex,
          selectedItem,
          getRootProps,
        }) => (
          <div>
            <label {...getLabelProps()}>Enter a pokemon </label>
            <div
              style={{ display: 'inline-block' }}
              {...getRootProps({}, { suppressRefError: true })}
            >
              <input {...getInputProps()} />
            </div>
            <ul {...getMenuProps()}>
              {isOpen

                ? items

                  .filter(item => !inputValue || item.value.includes(inputValue))
                  .map((item, index) => (
                    <li
                      {...getItemProps({
                        key: item.value,
                        index,
                        item,
                        style: {
                          backgroundColor:
                            highlightedIndex === index ? 'lightgray' : 'white',
                          fontWeight: selectedItem === item ? 'bold' : 'normal',
                        },
                      })}
                    >
                      {item.value}
                    </li>
                  ))
                : null}
            </ul>

<p></p>


          </div>
        )}
        
      </Downshift>,


    

    </div>
  );
}

我希望将进入控制台的数据插入到jsx中,并让它返回类似的内容

代码语言:javascript
复制
<div>
pokemon name {data.name}
pokemon stats {data.stats} 
</div>

我想不出如何共享我获取的数据,并将其放到控制台中,然后放到jsx中

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-25 22:32:04

将提取逻辑移动到另一个具有选择值依赖性的效果中。因此,当选择改变时,口袋妖怪将被重取。然后打印到控制台并更新组件的状态。如下所示:

代码语言:javascript
复制
const [selection, setSelection] = useState();
const [pokemon, setPokemon] = useState();

useEffect(() => {
  const fetchPokemon = async () => {
     const pokemon = await ...         
     console.log(pokemon);
     setPokemon(pokemon);
  }
  if (selection) {
    fetchPokemon();
  }
}, [selection])

<Downshift onChange={selection => setSelection(selection.value)} ...

{pokemon && <div>
  pokemon name {pokemon.name}
  pokemon stats {pokemon.stats} 
</div>}
票数 0
EN

Stack Overflow用户

发布于 2022-07-25 22:35:55

我注意到的第一件事是,useEffect中的依赖数组是空的,并且正在更新其中的状态。这会导致无限的呈现循环,这可能就是为什么在屏幕上看不到任何东西的原因。

代码语言:javascript
复制
    useEffect(() => {
    Pokemons();

    async function Pokemons() {
      const res = await fetch(
        `http://localhost:3001/pokemon/names`
      );
      const data = await res.json();
      setPokemonData(data);
    }
// Change here: 
  }, [pokemonData]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73115759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档