I've stored the form data in an object and sending this data through props using addedUser(userData); I've send a POST request to the firebase and then fetching the data to display the user's details in JSX, my AddUserForm.js have form and data sending to App.js for sending POST request, So how can I add my UserCard immediately right after form submission?
For now I'm reloading the page as the form submits, but I don't think that's the right way, right?
const onFormSubmit = () => {
addedUser(userData);
window.location.reload(true);
}
And I did this in my App.js to fetch and sending post request to firebase
function App() {
const [userData, setUserData] = useState([]);
const fetchData = useCallback(async () => {
try {
const responseData = await fetch(
"https://react-users-db-default-rtdb.asia-southeast1.firebasedatabase.app/users.json"
);
const data = await responseData.json();
const loadedData = [];
for (const key in data) {
loadedData.push({
key: key,
userName: data[key].userName,
userEmail: data[key].userEmail,
userBio: data[key].userBio,
userGithub: data[key].userGithub,
userImage: data[key].userImage,
});
}
setUserData(loadedData.reverse());
} catch (err) {
console.log(err);
}
}, []);
useEffect(() => {
fetchData();
}, [fetchData]);
async function recieveUserData(users) {
const responseData = await fetch(
"https://react-users-db-default-rtdb.asia-southeast1.firebasedatabase.app/users.json",
{
method: "POST",
body: JSON.stringify(users),
headers: {
"Content-Type": "application/json",
},
}
);
const data = await responseData.json();
console.log(data);
}
return (
<main>
<div className="form__wrap">
<AddUserForm addedUser={recieveUserData} />
</div>
<div className="user__wrap">
{userData.length > 0 ? (
<Users newUser={userData} />
) : (
<p>No user found</p>
)}
</div>
</main>
);
}
props..