Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Ajax请求获胜';t在react渲染功能中显示_Reactjs_Axios - Fatal编程技术网

Reactjs Ajax请求获胜';t在react渲染功能中显示

Reactjs Ajax请求获胜';t在react渲染功能中显示,reactjs,axios,Reactjs,Axios,我不知道为什么我的axios承诺的结果没有显示在渲染函数中。顺便说一下,我正在使用CreateReact应用程序工具 _getPrice() { const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot'; axios.get(url) .then(function (response) { //console.log(response.data.data.amount); let prices = respons

我不知道为什么我的axios承诺的结果没有显示在渲染函数中。顺便说一下,我正在使用CreateReact应用程序工具

_getPrice() {
const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
axios.get(url)
.then(function (response) {
    //console.log(response.data.data.amount);
    let prices = response.data.data.amount;
    return prices;
}) 
}

render() {
return(<div><h3> {this._getPrice()} </h3></div>);
}
\u getPrice(){
常量url=https://api.coinbase.com/v2/prices/BTC-USD/spot';
获取(url)
.然后(功能(响应){
//log(response.data.data.amount);
让价格=response.data.data.amount;
退货价格;
}) 
}
render(){
返回({this.\u getPrice()});
}

仅当组件的
状态或
道具发生更改时,React才会重新呈现组件。如果数据在渲染周期中更改,但未与这些变量交互,则更改不会显示

您可以保存承诺的结果,声明如下:

getInitialState() {
    return {prices: undefined}
}

componentDidMount() {
    const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
    axios.get(url)
    .then(function (response) {
        //console.log(response.data.data.amount);
        let prices = response.data.data.amount;
        this.setState({prices: prices});
    }.bind(this))
}

render() {
    return(<div><h3> {this.state.prices} </h3></div>);
}
getInitialState(){
返回{价格:未定义}
}
componentDidMount(){
常量url=https://api.coinbase.com/v2/prices/BTC-USD/spot';
获取(url)
.然后(功能(响应){
//log(response.data.data.amount);
让价格=response.data.data.amount;
this.setState({prices:prices});
}.绑定(本)
}
render(){
返回({this.state.prices});
}

仅当组件的
状态或
道具发生更改时,React才会重新呈现组件。如果数据在渲染周期中更改,但未与这些变量交互,则更改不会显示

您可以保存承诺的结果,声明如下:

getInitialState() {
    return {prices: undefined}
}

componentDidMount() {
    const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
    axios.get(url)
    .then(function (response) {
        //console.log(response.data.data.amount);
        let prices = response.data.data.amount;
        this.setState({prices: prices});
    }.bind(this))
}

render() {
    return(<div><h3> {this.state.prices} </h3></div>);
}
getInitialState(){
返回{价格:未定义}
}
componentDidMount(){
常量url=https://api.coinbase.com/v2/prices/BTC-USD/spot';
获取(url)
.然后(功能(响应){
//log(response.data.data.amount);
让价格=response.data.data.amount;
this.setState({prices:prices});
}.绑定(本)
}
render(){
返回({this.state.prices});
}

首先,你不能在渲染函数中调用函数作为返回,如果你想更新视图,你必须更新状态或道具…

首先,你不能在渲染函数中调用函数作为返回,如果你想更新视图,你必须更新状态或道具…

向服务器请求数据时,请求是异步的,这意味着服务器响应需要时间,浏览器将继续执行,而不是说,在您当前的实现中,您在
\u getPrice
函数中返回承诺,然后当服务器响应时,您不会对数据做任何处理

第二个问题是,react仅在状态或道具发生更改时才会重新渲染组件,而在当前的实现中,您不会更改任何更改

这里有一个你需要如何做才能让它工作的例子

class YourComponent extends Component {
  state = {
    prices: 0,
  };

  componentDidMount() {
    const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
    axios.get(url)
      .then((response) => {
        let prices = response.data.data.amount;
        this.setState({ prices });
      });
  }

  render() {
    const { prices } = this.state;

    return(
      <div>
        <h3> {prices} </h3>
      </div>
    );
  }
}
class YourComponent扩展组件{
状态={
价格:0,,
};
componentDidMount(){
常量url=https://api.coinbase.com/v2/prices/BTC-USD/spot';
获取(url)
。然后((响应)=>{
让价格=response.data.data.amount;
这个.setState({prices});
});
}
render(){
const{prices}=this.state;
返回(
{价格}
);
}
}

祝你好运

当向服务器请求数据时,请求是异步的,这意味着服务器需要时间响应,浏览器将继续执行,而不是说,在当前的实现中,您将在
\u getPrice
函数中返回一个承诺,然后当服务器响应时,您将不处理数据

第二个问题是,react仅在状态或道具发生更改时才会重新渲染组件,而在当前的实现中,您不会更改任何更改

这里有一个你需要如何做才能让它工作的例子

class YourComponent extends Component {
  state = {
    prices: 0,
  };

  componentDidMount() {
    const url = 'https://api.coinbase.com/v2/prices/BTC-USD/spot';
    axios.get(url)
      .then((response) => {
        let prices = response.data.data.amount;
        this.setState({ prices });
      });
  }

  render() {
    const { prices } = this.state;

    return(
      <div>
        <h3> {prices} </h3>
      </div>
    );
  }
}
class YourComponent扩展组件{
状态={
价格:0,,
};
componentDidMount(){
常量url=https://api.coinbase.com/v2/prices/BTC-USD/spot';
获取(url)
。然后((响应)=>{
让价格=response.data.data.amount;
这个.setState({prices});
});
}
render(){
const{prices}=this.state;
返回(
{价格}
);
}
}

祝你好运

好的。。您可以调用
this.\u getPrice()
componentDidMount
\u getPrice
函数中,获取数据后,可以使用
setState
将其存储在状态中,在渲染中可以显示来自状态的数据,如果数据不存在,则可以显示加载消息,这有用吗。。您可以调用
this.\u getPrice()
componentDidMount
\u getPrice
函数中,获取数据后,可以使用
setState
将其存储在状态中,在渲染中可以显示来自状态的数据,如果数据不存在,则可以显示加载消息,这有用吗?