未捕获(承诺中)错误:对象作为 React chid 无效。如果您打算渲染子集合,请使用数组
我试图渲染 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的渲染错误,请尝试将
{a}
替换为{a.login}
,如下所示:Your rendering is wrong, try to replace
{a}
by{a.login}
something like this:我以前也遇到过同样的问题。基本上这意味着 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}.