I am doing a program which gets information from the https://api.randomuser.me/ API. I got the fetch call working but my goal is to display the information as prefilled information in a form located in another file, but I don't know how I should export the variables and how to use them in the other file.
This is my FetchCall.js:
import Axios from 'axios'
import { useState } from 'react';
function FetchCall() {
const[name,setName] = useState("")
const[birth,setBirth] = useState("")
const[email,setEmail] = useState("")
const[insuranceNo, setInsuranceNo] = useState("")
const[phoneNo, setPhoneNo] = useState("")
const getInfo = () => {
Axios.get("https://api.randomuser.me/").then(
(response) => {
setName(response.data.results[0].name.first + " " + response.data.results[0].name.last);
setBirth(response.data.results[0].dob.date);
setEmail(response.data.results[0].email)
setInsuranceNo(response.data.results[0].login.sha1)
setPhoneNo(response.data.results[0].phone)
}
);
};
return(
<div>
Hello
<button onClick={getInfo}>Get User</button>
<div>
{name}
</div>
<div>
{birth}
</div>
<div>
{email}
</div>
<div>
{insuranceNo}
</div>
<div>
{phoneNo}
</div>
</div>
);
}
export default FetchCall;
And here is where I want to export the variables (name, birth,email, insuranceNo,phoneNo)
import FetchCall from './fetchCall';
function InputTextFilled() {
return (
<div className="inputText has-text-left">
<fieldset disabled>
<div className="field">
<div className="control">
<p className="label">Full Name</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<a className="label">Date of Birth</a>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">National Insurance Number</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">Email Address</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
<div className="control mt-5">
<p className="label">Telephone Number</p>
<input className="input" type="text" value="VARIABLEHERE" />
</div>
</div>
</fieldset>
</div>
);
}
export default InputTextFilled;