0

i have a fetch that shows the below results. Now i want to show the fetch in the return statement(in the div results). 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.

(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)
  }
}

1

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Do you also have an idea how to connect the fetch to the <input>. So that the value of the input is the url parameter of the fetch? I am creating an auto suggest search bar.
@Melissa If you need fetch data on every input, you can just transfer your fetch logic (code inside useEffect and you can delete useEffect) in handleChange (use e.target.value like url parameter)
1

create state through React.useState, change it when you get result. 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)
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.