简体   繁体   English

如何在 React 的 return 语句中使用 fetch 结果?

[英]How to use fetch result in return statement in React?

i have a fetch that shows the below results.我有一个显示以下结果的获取。 Now i want to show the fetch in the return statement(in the div results).现在我想在 return 语句中显示 fetch(在 div 结果中)。 Has anyone an idea how to do that.有谁知道如何做到这一点。 I tried it with a map function because i though the fetch is an array, but i failed.我用 map function 尝试了它,因为我虽然 fetch 是一个数组,但我失败了。

 (9) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 0: {label: "Hello world", href: "#hello-world"} 1: {label: "John Doe", href: "#john-doe"} 2: {label: "Jane Doe", href: "#jane-doe"} 3: {label: "Foo", href: "#foo"} 4: {label: "Bar", href: "#bar"} 5: {label: "Baz", href: "#baz"} 6: {label: "Henry Ford", href: "#henry-ford"} 7: {label: "Gordon Ramsay", href: "#gordon-ramsay"} 8: {label: "Angela Merkel", href: "#angele-merkel"} length: 9__proto__: Array(0)
 export function AutoSuggestForm({ onChange, value }) { React.useEffect(() => { const myFetch = fetch('http://localhost:8000/api/auto-suggest?input=input') myFetch.then(response => response.json()).then(console.log) }) return ( <div className={styles.component}> <input onChange={handleChange} {...{ value }} className={styles.input} type='search' placeholder='search' /> <div className={styles.results} /> </div> ) function handleChange(e) { onChange(e.target.value) } }

https://reactjs.org/docs/hooks-state.html https://reactjs.org/docs/hooks-state.html

export function AutoSuggestForm({ onChange, value }) {
    const [data, setData] = React.useState([]);

    React.useEffect(() => {
        const myFetch = fetch('http://localhost:8000/api/auto-suggest?input=input');
        myFetch.then(response => response.json()).then(setData);
    });

    return (
        <div className={styles.component}>
            <input onChange={handleChange} {...{ value }} className={styles.input} type="search" placeholder="search" />
            <div className={styles.results}>
                {data.map(d => (
                    <div key={d.label}>{d.label}</div>
                ))}
            </div>
        </div>
    );
    function handleChange(e) {
        onChange(e.target.value);
    }
}

create state through React.useState, change it when you get result.通过 React.useState 创建 state,当你得到结果时改变它。 This is basis of react这是反应的基础

export function AutoSuggestForm({ onChange, value }) {
  const [results, changeResults] = React.useState([])
  React.useEffect(() => {
    const myFetch = fetch('http://localhost:8000/api/auto-suggest?input=input')
    myFetch.then(response => response.json()).then(res => changeResults(res))
  })
  return (
    <div className={styles.component}>
      <input onChange={handleChange} {...{ value }} className={styles.input} type='search' placeholder='search' />
      <div className={styles.results} >
        {results.map((result, i) => <span key={i}>{result}</span>}
      </div>
    </div>
  )
  function handleChange(e) {
    onChange(e.target.value)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提取后如何返回结果? - How to return result after fetch? 如何在 React 中使用 for/in 语句和 fetch() - How to work with for/in statement and fetch() in React 如何使用 IF 语句避免用户输入错误的城市名称时 Weather API 获取结果崩溃? - How to use the IF statement to avoid the Weather API fetch result crashing once the user types wrong the city name? 如何获取结果并将结果传递给反应中的下一个获取? - How can I fetch and pass the result to the next fetch in react? 等到Promise解决后将其结果传递给React return语句 - Wait until a Promise is resolved to pass its result to a React return statement 如何以及何时使用 Return 语句? - How and when to use Return statement? 如何在 function 中使用引用嵌套 function 内部参数的条件语句在 javascript 中返回结果? - How can use a conditional statement inside of a function that references the parameter inside of a nested function to return a result in javascript? 如何在React中延迟返回直到完成获取 - how to delay the return in React until fetch is done 如何使用Backbone Collection获取此JSON结果? - how to use Backbone Collection to fetch this JSON result? 如何使用正则表达式获取 XML 导致 javascript - How to use regex for with a fetch XML result in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM