如何在使用 axios 和 ReactJS 做出反应 select 后获取数据?
How to get a data after react select using axios and ReactJS?
我是 ReactJS 的新手,我想通过反应 select 获取产品名称 select 之后的价格值并显示它。
我的方法:
constructor(props) {
super(props);
this.state = {
PrixV: ""
}
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState({
PrixV: response.data
});
}
console.log(response.data)
}).catch(error => {
console.error(error);
});
}
我尝试通过以下方式读取此值:
<p>{this.state.PrixV} </p>
当我 运行 它时,价格栏上没有显示任何内容,在我 select 产品名称之后,我得到:
Objects are not valid as a React child (found: object with keys {PrixV}). If you meant to render a collection of children, use an array instead.
in p (at AjouterFacture.js:383)
和控制台 returns :
[{…}]0: {PrixV: "250.000"}length: 1__proto__: Array(0)
如何读取和显示它?
constructor(props) {
super(props);
this.state = {
mydata: ''
};
productNameSelected(selectprdt) {//not sure from where you will get selectprdt variable , but something like this will work
axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState( {mydata : response.data});
}
console.log(response.data)
})
}
<p>{this.state.mydata} </p> //my data should not be a object , so you need to make string if you wnat to directly use it here
编辑
我可以看到你能够得到回应,但反应仍然给出错误:-
将段落代码更改为
<p>{this.state.PrixV[0].PrixV} </p>
或者 好的方法是正确设置数据
让段落相同
<p>{this.state.PrixV} </p>
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data && response.data[0]) {
this.setState({
PrixV: response.data[0].PrixV
});
}
console.log(response.data)
}).catch(error => {
console.error(error);
});
}
您收到错误是因为当前您将 PrixV 作为数组(对象)而不是原始数据类型获取。
希望这能解决问题。
你一定要用地图,试试把代码改成这样:
constructor(props) {
super(props);
this.state = {
Prix: []};
render() {
let {
Prix
} = this.state.Prix;
return (
<td>
{ this.state.Prix.map((pr, k) =>
<p key={k} >{pr.PrixV} </p>
)}
</td>
);}
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState({
Prix: response.data
});
}
}).catch(error => {
console.error(error);
});
}
希望对您有所帮助。
我是 ReactJS 的新手,我想通过反应 select 获取产品名称 select 之后的价格值并显示它。
我的方法:
constructor(props) {
super(props);
this.state = {
PrixV: ""
}
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState({
PrixV: response.data
});
}
console.log(response.data)
}).catch(error => {
console.error(error);
});
}
我尝试通过以下方式读取此值:
<p>{this.state.PrixV} </p>
当我 运行 它时,价格栏上没有显示任何内容,在我 select 产品名称之后,我得到:
Objects are not valid as a React child (found: object with keys {PrixV}). If you meant to render a collection of children, use an array instead.
in p (at AjouterFacture.js:383)
和控制台 returns :
[{…}]0: {PrixV: "250.000"}length: 1__proto__: Array(0)
如何读取和显示它?
constructor(props) {
super(props);
this.state = {
mydata: ''
};
productNameSelected(selectprdt) {//not sure from where you will get selectprdt variable , but something like this will work
axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState( {mydata : response.data});
}
console.log(response.data)
})
}
<p>{this.state.mydata} </p> //my data should not be a object , so you need to make string if you wnat to directly use it here
编辑
我可以看到你能够得到回应,但反应仍然给出错误:- 将段落代码更改为
<p>{this.state.PrixV[0].PrixV} </p>
或者 好的方法是正确设置数据 让段落相同
<p>{this.state.PrixV} </p>
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data && response.data[0]) {
this.setState({
PrixV: response.data[0].PrixV
});
}
console.log(response.data)
}).catch(error => {
console.error(error);
});
}
您收到错误是因为当前您将 PrixV 作为数组(对象)而不是原始数据类型获取。 希望这能解决问题。
你一定要用地图,试试把代码改成这样:
constructor(props) {
super(props);
this.state = {
Prix: []};
render() {
let {
Prix
} = this.state.Prix;
return (
<td>
{ this.state.Prix.map((pr, k) =>
<p key={k} >{pr.PrixV} </p>
)}
</td>
);}
PrixDisplay(selectprdt) {
return axios.get("/app/getPrixprod/" + selectprdt).then(response => {
if (response && response.data) {
this.setState({
Prix: response.data
});
}
}).catch(error => {
console.error(error);
});
}
希望对您有所帮助。