JavaScript
x
19
19
1
import React, { useState } from "react";
2
import { MineralOutput } from "./Quantity";
3
4
export function Calculation(props){
5
6
const [age, setAge] = useState('');
7
const [gender, setGender] = useState('');
8
const [userid, setUserid] = useState('');
9
10
function handleChange(e){
11
setAge(e.target.value);
12
};
13
function handleChangeGender(e){
14
setGender(e.target.value);
15
};
16
function handleChangeUserid(e){
17
setUserid(e.target.value);
18
};
19
here I want to invoke the MineralOutput component after the submit button and pass in the age, gender, and userid with state.
JavaScript
1
31
31
1
function handleSubmit(e){
2
3
<MineralOutput age={age} gender={gender} userid={userid} />
4
5
};
6
7
8
9
return(
10
<form onSubmit={handleSubmit}>
11
<div><label>age
12
<input type="text" onChange={handleChange}
13
value={age}
14
/></label><br></br>
15
<label>gender
16
<input type="text" onChange={handleChangeGender}
17
value={gender}
18
/></label><br></br>
19
<label>userid
20
<input type="text" onChange={handleChangeUserid}
21
value={userid}
22
/></label><br></br>
23
<input type="submit" value="submit" />
24
</div></form>
25
)
26
27
28
29
30
}
31
This is the component that I imported.
JavaScript
1
28
28
1
export function MineralOutput(age, gender, userid){
2
if (age >= 71 && gender == 1) {
3
4
let body = {
5
mineralslId: 3,
6
userId : userid,
7
choline: 425,
8
magnesium: 420,
9
calcium: 1200,
10
zinc: 8,
11
iron: 8,
12
copper: 900,
13
selenium: 55,
14
manganese: 1.8,
15
phosphorus: 700,
16
potassium: 2600,
17
iodine: 150,
18
chromium: 20,
19
flouride: 3,
20
sodium: 2300,
21
chloride: 1.8
22
};
23
24
APIPost("minerals", body);
25
26
}
27
}
28
When I click the submit button, I get a 404 status code error. I’m using Axios for requests and it has already worked before when I wanted to post inside a class. But, this time I want to try and post by importing a component and passing in the values.
Advertisement
Answer
Solved by @windowsill. My “Component” MineralOutput wasn’t a component, but a function. So instead of <MineralOutput />
I just needed to call it like MineralOutput(1,2,3)