Im trying to pass data from React --> Flask --> React and show it in the DOM.
So far I been able to:
- Send data from React to Flask and display the output on the console. (POST)
- Send data from flask to React and display it on the DOM (GET)
So now my confusion is how do I put these 2 together. Here is my React code to send my data to Flask Username.js (POST)
##REACT
class Content extends Component {
login = (e) => {
e.preventDefault();
axios
.post("/username", {
username: document.getElementById("username").value,
})
.then((res) => {
console.log( res.data);
});
};
render() {
return(
<div className="p"> <User />
<form onSubmit={this.login}>
<p>
<label htmlFor="email">Email</label>
<input
type="username"
className="w3-input w3-border"
id="username"
/>
</p>
<p>
<button type="submit" className="w3-button w3-blue">
Login
</button>
</p>
</form>
</div>
);
}
}
And here is how I receive my data on flask
###FLASK
@app.route("/username", methods=['POST'])
def login():
username = request.json["username"]
#username = request.args.get('username')
return {"username": 'This is the backend -- >'+ username }
When adding anything on my input label I can see it on the console. Now what I want is to get the result that I added on the input in the DOM. This can be on another page maybe?
Do I need to create a new route and pass the data from the /username? and then make an API call on my react app to this new route? How can I pass this username output to another route? Is this the right way of doing it?
Please advice thank you