Right now, I have a dictionary like this: m = {"score": x, "keywords": {"Mrs. Johnson": 87}}
that's being passed from my backend (flask) to my frontend (react), and I want it to be an object when it's passed into the frontend so I can use it as an object and get the value of "score"
by using .score
.
I'm facing two problems right now, I'm not sure how to pass in a dictionary and have it as an object in react; currently I'm using json.dumps()
which I know turns stuff into strings, is there something like that for objects?. Second, once it is passed in as an object, how do I update a pre-existing object (it'll be blank like this data: {}
) so that it will be equal to the object I passed in from my backend? Here is my code so far:
BACKEND:
def post(self):
x = 67
m = {"score": x, "keywords": {"Mrs. Johnson": 87}}
return json.dumps(m)
FRONTEND:
class Test extends Component {
constructor() {
super()
this.state = {
data: {}
}
}
const options = {
method: "POST",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(this.state.text) // Irrelevant
};
console.log("hi")
fetch("http://127.0.0.1:5000/", options)
.then(response=> response.JSON())
.then(json => this.setState({data: json}), console.log(this.state.data))
}