Javascript 显示获取数据和呈现-React JS
我能够将数据保存到我的数据库中。但是,我想知道在运行fetch请求后如何在屏幕上显示/呈现fetch数据 当我添加数据时,我能够在我的页面上推动渲染。但我想要的是,一旦我运行了fetchdata函数,我如何将得到的响应呈现到屏幕上 当我Javascript 显示获取数据和呈现-React JS,javascript,reactjs,Javascript,Reactjs,我能够将数据保存到我的数据库中。但是,我想知道在运行fetch请求后如何在屏幕上显示/呈现fetch数据 当我添加数据时,我能够在我的页面上推动渲染。但我想要的是,一旦我运行了fetchdata函数,我如何将得到的响应呈现到屏幕上 当我console.log(Json.data.shipping) PS:带React JS的初学者 下面是我如何保存数据的方法 state = { shippings: userData, addNewData: { nam
console.log(Json.data.shipping)
PS:带React JS的初学者
下面是我如何保存数据的方法
state = {
shippings: userData,
addNewData: {
name: '',
phone: ''
},
};
addData() {
const { name,phone} = this.state.addNewData;
if (name!== '' && phone = "") {
let newData = {
...this.state.addNewData,
id: new Date().getTime()
}
let shippings = this.state.shippings;
fetch( 'http://facicla:5000/api', {
method:'post',
/* headers are important*/
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
body: JSON.stringify(this.state.addNewData)
})
.then(response => {
return response.json();
shippings.push(newData);
NotificationManager.success('Sucess!');
})
}
}
export default [
{
name: 'Shipping-Car',
phone: '001-72342-2342',
} ]
fetchAllData(){
return this.fetchPost().then(([response,json]) => {
if(response.status === 200)
{
console.log(json.data.shipping)
0: { name: "Samsung A10", phone: "001-2342-23429"}
1: {name: "Iphone Xs", phone: "001-12193-1219"}
}
})
}
fetchPost(){
const URL = 'http://facicla:5000/api';
return fetch(URL, {method:'GET',headers:new Headers ({
'Accept': 'application/json',
'Content-Type': 'application/json',
})})
.then(response => Promise.all([response, response.json()]));
}
用户数据
state = {
shippings: userData,
addNewData: {
name: '',
phone: ''
},
};
addData() {
const { name,phone} = this.state.addNewData;
if (name!== '' && phone = "") {
let newData = {
...this.state.addNewData,
id: new Date().getTime()
}
let shippings = this.state.shippings;
fetch( 'http://facicla:5000/api', {
method:'post',
/* headers are important*/
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
body: JSON.stringify(this.state.addNewData)
})
.then(response => {
return response.json();
shippings.push(newData);
NotificationManager.success('Sucess!');
})
}
}
export default [
{
name: 'Shipping-Car',
phone: '001-72342-2342',
} ]
fetchAllData(){
return this.fetchPost().then(([response,json]) => {
if(response.status === 200)
{
console.log(json.data.shipping)
0: { name: "Samsung A10", phone: "001-2342-23429"}
1: {name: "Iphone Xs", phone: "001-12193-1219"}
}
})
}
fetchPost(){
const URL = 'http://facicla:5000/api';
return fetch(URL, {method:'GET',headers:new Headers ({
'Accept': 'application/json',
'Content-Type': 'application/json',
})})
.then(response => Promise.all([response, response.json()]));
}
获取数据
state = {
shippings: userData,
addNewData: {
name: '',
phone: ''
},
};
addData() {
const { name,phone} = this.state.addNewData;
if (name!== '' && phone = "") {
let newData = {
...this.state.addNewData,
id: new Date().getTime()
}
let shippings = this.state.shippings;
fetch( 'http://facicla:5000/api', {
method:'post',
/* headers are important*/
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
body: JSON.stringify(this.state.addNewData)
})
.then(response => {
return response.json();
shippings.push(newData);
NotificationManager.success('Sucess!');
})
}
}
export default [
{
name: 'Shipping-Car',
phone: '001-72342-2342',
} ]
fetchAllData(){
return this.fetchPost().then(([response,json]) => {
if(response.status === 200)
{
console.log(json.data.shipping)
0: { name: "Samsung A10", phone: "001-2342-23429"}
1: {name: "Iphone Xs", phone: "001-12193-1219"}
}
})
}
fetchPost(){
const URL = 'http://facicla:5000/api';
return fetch(URL, {method:'GET',headers:new Headers ({
'Accept': 'application/json',
'Content-Type': 'application/json',
})})
.then(response => Promise.all([response, response.json()]));
}
渲染
render() {
const { shippings, addNewData} = this.state;
return (
<div className="wrapper">
<div className="row row-eq-height">
{shippings.map((shipping, key) => (
<div className="col-sm-3 col-md-3 col-lg-3" key={key}>
<div className="d-flex justify-content-between">
<h5 className="fw-bold">{shipping.name}</h5></a>
<h5 className="fw-bold">{shipping.phone}</h5></a>
</div>
</div>
))}
</div>
}
render(){
const{shippings,addNewData}=this.state;
返回(
{shippings.map((shipping,key)=>(
{shipping.name}
{shipping.phone}
))}
}
试试这个:
fetchAllData(){
return this.fetchPost().then(([response,json]) => {
if(response.status === 200)
{
console.log(json.data.shipping)
this.setState(
{ shippings: Object.values(json.data.shipping)
//or shippings: json.data.shipping
}
)
//0: { name: "Samsung A10", phone: "001-2342-23429"}
//1: {name: "Iphone Xs", phone: "001-12193-1219"}
}
})
}
只能使用
this.setState()
更改状态,这包括附加到数组。将shippings.push(newData);
替换为this.setState({shippings:[…shippings,newData]})
So,您想在shipping right中存储json.data.shipping吗?@AddWebSolutionPvtLtd是的,我想在页面加载时显示它……当用户加载页面时,我运行fetch data函数,因此页面必须填充`json.data.shipping`@RoboPHP尝试并运行在组件生命周期方法componentD中获取数据的函数idMount()
。您可以在那里填充状态object@minus.273是的,我已经有运行函数的componentDidMount()
。我的问题是如何填充要在我的页面上呈现的状态对象