Currently I'm using Reactjs as the frontend for my website and a csv file as my backend database (Don't ask why. It's the requirement). I created a local http web server (written in Python) and it's working as desired. My question is how do I put the content of each cell of my csv file into a variable(or variables) and display them at wherever I want them to be on the webpage? There seems to be a problem with variable scope.
Here's what I got so far:
import React, { Component } from "react";
import $ from 'jquery';
function getData (data) {
this.id = data.id;
this.name = data.name;
this.age = data.age;
}
export default class Profile extends Component {
constructor (props) {
super(props);
this.state = {
jsonlist: null,
isNull: true
}
this.onClick = this.handleClick.bind(this);
}
handleClick() {
var jl;
$.post("http://127.0.0.1:8080",
function(json){
var parsed = JSON.parse(json);
jl = parsed.list.map(function(data){
return new getData(data);
});
this.setState(
isNull: false,
jsonlist: jl
);
});
}
render() {
return (
<div>
<button onClick = {this.onClick}>GET CONTENT</button>
{ this.state.isNull
? null
:
<ul>
<div>name: { this.state.jsonlist["0"].name } </div>
<div>age: { this.state.jsonlist["0"].age } </div>
</ul>
}
</div>
)
}
}
I know I can't set jsonlist with the value of jl (a json object). Is there a better way to do that? And the value of jsonlist in the return function is always null. What should I do with that?
csv file content for this webpage:
id || name || age
1 || A || 22
2 || B || 32
3 || C || 29
4 || D || 24
Can somebody give me an example using axios instead of jquery?