I have my node js server up and running and I handle query requests like this :
controller.js
const getUsers = async (req, res, next) => {
const age = req.query.age || 1;
const lessage = req.query.minAge || 1000000000;
const search = req.query.s || '';
let users;
try {
users = await User.find({ age: { $gt: age, $lt: lessage }, name: { $regex: search } });
} catch (e) {
console.log('Could not find users : ' + e);
next(e);
}
res.json(users);
};
I check if there is query value in the url then use that in mongoose find() method if not use the default value .
Now my question is how can I handle or basically send these queries in react with radio buttons and inputs and checkboxes ? I've tried to handle it without any library and everytime I missed something or got caught up in a bug .
I tried like this :
const what = loc.length < 1 ? '?' : '&';
const [ name, setName ] = React.useState('');
const [ searchName, setSearchName ] = React.useState('');
const [ radio, setRadio ] = React.useState('');
const [ place, setPlace ] = React.useState('');
<select
onChange={(e) => {
setPlace(e.target.value);
setLoc((prev) => prev + place);
}}
>
<option value="">Select place</option>
<option value={`${what}place=us`}>US</option>
<option value={`${what}place=uk`}>UK</option>
</select>
{place}
<form
onChange={(e) => {
setRadio(e.target.value);
setLoc((prev) => prev + radio);
}}
>
<input type="radio" name="gender" value={`${what}age=22`} /> Age over 22 <br />
<input type="radio" name="gender" value={`${what}ageMin=22`} /> Age lower than 22 <br />
</form>
{radio}
<input
type="text"
value={name}
onChange={(e) => {
setName(e.target.value);
}}
/>
There is no tutorial nor an article on how to send multiple query requests with different options in react .
So How can I handle sending query requests to the backend with react js ?