未捕获(承诺中)错误:对象作为 React chid 无效。如果您打算渲染子集合,请使用数组

发布于 2025-01-09 15:55:07 字数 1760 浏览 0 评论 0原文

我试图渲染 polkadot 浏览器扩展的输出,但我收到此错误未捕获(承诺中)错误:对象作为 React 子项无效(找到:带有键 {address,meta,type} 的对象) 。如果您打算渲染子集合,请改用数组。

我看到它希望我使用数组(或地图?),但我不确定如何实现这个

有什么想法吗?

import ReactDOM from 'react-dom';
import './index.css';
import reportWebVitals from './reportWebVitals';
import {
  web3Accounts,
  web3Enable,
  web3FromAddress,
  web3ListRpcProviders,
  web3UseRpcProvider
} from '@polkadot/extension-dapp';

class UserComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      allInjected: [],
      accountsInfo: []
    };
  }

  async componentDidMount() {
    this.handleButtonClick();
  }

  handleButtonClick = () => {
    const getExtensionInfo = async () => {
      const allInjected  = await web3Enable('test');
      const allAccounts = await web3Accounts();
      //const account = allInjected;
      //const info = address;
      this.setState({
        allInjected,
        allAccounts
      });

    };
    getExtensionInfo();
  };

  render() {
    const allInjected = this.state.allAccounts?.map((a, i) => (
      <li key={i} className="list-group-item">{a}</li>
    ));
    const allAccounts = this.state.allAccounts?.map((a, i) => (
      <li key={i} className="list-group-item">{a}</li>
    ));

    return (
      <div>
        <h1>A user</h1>
        <p>{allInjected}</p>
        <h1>{allAccounts}</h1>
        <button onClick={this.handleButtonClick}>Get Info</button>
      </div>
    );
  }
}

ReactDOM.render(
  <React.StrictMode>
    <UserComponent />
  </React.StrictMode>,
  document.getElementById('root')
);


reportWebVitals();

Im trying to render the output of the polkadot browser extension but i'm getting this error Uncaught (in promise) Error: Objects are not valid as a React child (found: object with keys {address, meta, type}). If you meant to render a collection of children, use an array instead.

I see that it wants me to use an array (or a map?) but i'm unsure of how to implement this

Any ideas?

import ReactDOM from 'react-dom';
import './index.css';
import reportWebVitals from './reportWebVitals';
import {
  web3Accounts,
  web3Enable,
  web3FromAddress,
  web3ListRpcProviders,
  web3UseRpcProvider
} from '@polkadot/extension-dapp';

class UserComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      allInjected: [],
      accountsInfo: []
    };
  }

  async componentDidMount() {
    this.handleButtonClick();
  }

  handleButtonClick = () => {
    const getExtensionInfo = async () => {
      const allInjected  = await web3Enable('test');
      const allAccounts = await web3Accounts();
      //const account = allInjected;
      //const info = address;
      this.setState({
        allInjected,
        allAccounts
      });

    };
    getExtensionInfo();
  };

  render() {
    const allInjected = this.state.allAccounts?.map((a, i) => (
      <li key={i} className="list-group-item">{a}</li>
    ));
    const allAccounts = this.state.allAccounts?.map((a, i) => (
      <li key={i} className="list-group-item">{a}</li>
    ));

    return (
      <div>
        <h1>A user</h1>
        <p>{allInjected}</p>
        <h1>{allAccounts}</h1>
        <button onClick={this.handleButtonClick}>Get Info</button>
      </div>
    );
  }
}

ReactDOM.render(
  <React.StrictMode>
    <UserComponent />
  </React.StrictMode>,
  document.getElementById('root')
);


reportWebVitals();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埖埖迣鎅 2025-01-16 15:55:07

您的渲染错误,请尝试将 {a} 替换为 {a.login} ,如下所示:

const allInjected = this.state.allAccounts?.map((a, i) => (
  <li key={i} className="list-group-item">{a.login}</li> // <=== You are using object `a`, try to change it by your field
));
const allAccounts = this.state.allAccounts?.map((a, i) => (
  <li key={i} className="list-group-item">{a.login}</li> // <=== You are using object `a`, try to change it by your field
));

Your rendering is wrong, try to replace {a} by {a.login} something like this:

const allInjected = this.state.allAccounts?.map((a, i) => (
  <li key={i} className="list-group-item">{a.login}</li> // <=== You are using object `a`, try to change it by your field
));
const allAccounts = this.state.allAccounts?.map((a, i) => (
  <li key={i} className="list-group-item">{a.login}</li> // <=== You are using object `a`, try to change it by your field
));
痴骨ら 2025-01-16 15:55:07

我以前也遇到过同样的问题。基本上这意味着 a 是一个对象,react 会很困惑如何渲染它,例如,如果 a = { "user": "Mary", "login": true},react 将不知道是否应该将其渲染为一个字符串,或者只返回值或键。

为了解决该错误,您可以指定要显示的值/键,例如 {a.user}。

I've had the same issue before. Basically it means a is an object, and react will be confused how to render it, for example if a = { "user": "Mary", "login": true}, react won't know if it should render it as a string, or just return the values or the keys.

In order to resolve the error you can specify the value / key you'd like to display, for example {a.user}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文