简体   繁体   English

我收到一个错误:对象作为 React 子对象无效(找到:[object Promise])

[英]I got an Error: Objects are not valid as a React child (found: [object Promise])

I am kind of new to React.我对 React 有点陌生。 And I got this Error: Objects are not valid as a React child (found: [object Promise]).我得到了这个错误:对象作为 React 子对象无效(找到:[object Promise])。 And I am not sure how to fix this issue.我不知道如何解决这个问题。 Can you guys take a look?大家可以看看吗? Thanks!谢谢!

Here's my code.这是我的代码。

import React, { Component } from "react";
import "./App.css";

async function randomVerse() {
  const url = "https://labs.bible.org/api/?passage=random&type=json";
  const passage = await fetch(url);
  const passageJson = await passage.json();

  return passageJson;
}

const displayRandomVerse = async () => {
  let result = await randomVerse();
  let verse = "";
  verse += result[0].text;
  return verse;
};

const returnVerse = displayRandomVerse();

class App extends Component {
  constructor() {
    super();
    this.state = {
      verse: null
    };
  }

  componentDidMount() {
    this.setState({verse: returnVerse});
  };

  render() {
    return (
      <div className="App">
        <h1>Good Morning, XXX!</h1>
        <h3>Bible verse : {this.state.verse} </h3>
      </div>
    );
  }
}

export default App;

You have to set await in const returnVerse = await displayRandomVerse();你必须在const returnVerse = await displayRandomVerse(); 中设置 await . . This should give you the value这应该给你价值

You need to fetch and await in your componentDidMount method您需要在 componentDidMount 方法中获取并等待

async function randomVerse() {
      const url = "https://labs.bible.org/api/?passage=random&type=json";
      const passage = await fetch(url);
      const passageJson = await passage.json();
    
      return passageJson;
    }
    
    const displayRandomVerse = async () => {
      let result = await randomVerse();
      let verse = "";
      verse += result[0].text;
      return verse;
    };
    
    
    class App extends Component {
      constructor() {
        super();
        this.state = {
          verse: null
        };
      }
    
      async componentDidMount() {  //Notice changes in this section
        this.setState({verse: await displayRandomVerse()});
      };
    
      render() {
        return (
          <div className="App">
            <h1>Good Morning, XXX!</h1>
            <h3>Bible verse : {this.state.verse} </h3>
          </div>
        );
      }
    }
    
    export default App;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React 错误“对象作为 React 子项无效(找到:[object Promise])” - React error "Objects are not valid as a React child (found: [object Promise])" 错误:对象在 reactjs 中作为 React 子对象无效(找到:[object Promise]) - Error: Objects are not valid as a React child (found: [object Promise]) in reactjs 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) 对象作为 React 子级无效(发现:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) 对象无效,无法作为React子对象(找到:[object Promise]) - Objects not valid as a React child (found: [object Promise]) 对象作为 React 子级无效(找到:[object Promise])。 当我想将 state 设置为 true 时显示错误 - Objects are not valid as a React child (found: [object Promise]). Showing error when I want to set a state to true 对象在反应渲染中作为 React 子级无效(找到:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) in react render 对象作为 React 子对象无效(找到:[object Promise])。 但我没有回报承诺? - Objects are not valid as a React child (found: [object Promise]). But I am not returning a promise? 无法在 JSX 中渲染对象。 抛出错误对象作为 React 子对象无效(发现:[object Promise]) - Can't render Objects inside JSX. Throwing error Objects are not valid as a React child (found: [object Promise]) React 中的异步函数触发错误:对象作为 React 子对象无效(找到:[object Promise]) - Async function in React triggers error: Objects are not valid as a React child (found: [object Promise])
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM