gpt4 book ai didi

javascript - 如何传递从服务器返回的数据,然后将返回的数据转移到另一个路由?

转载 作者:行者123 更新时间:2023-12-03 01:18:07 26 4
gpt4 key购买 nike

Hi, I am new to react. I made a server call and then successfully got a response. Now, I am trying to pass the same response to another route and display there. I am unable to understand, how do we do this in react, like in Ember there's a method this.transitionTo('routepath', {data}) and then this data is available in routepath in models(params). How is this possible in react? I am bound to not using any Redux or any other state container. My code is below:

import React, { Component } from 'react'
import axios from 'axios'

class Ernform extends Component {
constructor (props) {
super();
this.state = {
category: '',
zipcode: '',
age: '',
gender: '',
key: '',
data: null
};
}

onChange = (e) => {
this.setState({ [e.target.name]: e.target.value });
}

getPolicies = (e) => {
e.preventDefault();
const { category, zipcode, age, gender, key } = this.state;
axios.post(`http://localhost:4000/api/v1/quote`, { category, zipcode, age, gender, key })
.then(response => {
this.setState({data: response}); // I am setting response from server to the state property data
this.props.history.push('/details', {data: this.state.data})//I am trying to pass the data and trying to transition here.
});
}

render() {
const { category, zipcode, age, gender } = this.state;
return (
<div>
<form onSubmit={this.getPolicies}>
<label>
Category: <input type="text" name="category" value={category} onChange={this.onChange}/>
</label>
<label>
Zipcode: <input type="text" name="zipcode" value={zipcode} onChange={this.onChange}/>
</label>
<label>
Age: <input type="text" name="age" value={age} onChange={this.onChange}/>
</label>
<label>
Gender: <input type="text" name="gender" value={gender} onChange={this.onChange}/>
</label>
<button className="btn btn-primary btn-lg lead" type="submit">Get Policies</button>
</form>
</div>
);
}
}

export default Ernform

My router is this:

import React from 'react'
import { Switch, Route } from 'react-router-dom'
import Ernform from './Ernform';
import Ernentry from './Ernentry'
import Erndetails from './Erndetails';

const Routing = () => (
<main>
<Switch>
<Route exact path='/' component={Ernentry}/>
<Route path='/auto' component={Ernform}/>
<Route path='/life' component={Ernform}/>
<Route path='/details' component={Erndetails}/> // added this route.
</Switch>
</main>
)

export default Routing

My details route component is this

import React, { Component } from 'react'


class Erndetails extends Component {
constructor(props) { // Where would i get the data i transition with from that route.
console.log(props);
super()
}
render() {
return (
<div></div>
)
}
}

export default Erndetails

Thanks a lot guys in Advance.

最佳答案

有两种解决方案可以解决您的问题,

I) 第一个解决方案是以这种方式将推送对象中的数据与路由一起传递, this.props.history.push({
pathname: '/details',
state: { detail: response.data }
})

而不是你传递的内容。

在您的详细信息路由组件中,您可以使用 props.location.state.detail 获取数据在你的构造函数中或 this.props.location.state.detail在 componentDidMount 生命周期中。

2) 第二种解决方案是使用 localStorage.setItem('token', data) 将其存储在本地存储中然后使用 localStorage.getItem('token') 访问它

关于javascript - 如何传递从服务器返回的数据,然后将返回的数据转移到另一个路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51902608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com