-1
   import B from './B.js'; 
   const Test = () => {
      
      async function getResults() {
        await axios
          .get(`http://localhost:2000/?url=${link}`)
          .then((response) => {
            <B data={response.data} />
          });
      }
      return (
        <>
          <div> 
           <button onClick={getResults}> Click </button>
         </div>
        </>
          )

I was trying to pass the axios data as props to my component B. But in console I got undefined value. How to solve this issue. Should I pass the props inside the return area of the Test Component only?

Amit Kumar
  • 43
  • 2
  • 7

1 Answers1

0

You need to use useState to store data

import B from './B.js';
import { useState } from 'react'
const Test = () => {
  const [data, setData] = useState({})
  async function getResults () {
    await axios
      .get(`http://localhost:2000/?url=${link}`)
      .then((response) => {
        setData(response.data)
      });
  }
  return (
    <>
      <div>
        <button onClick={getResults}> Click </button>
        <B data={data} />

      </div>
    </>
  )
}