简体   繁体   English

React 错误,当我在孩子中使用 Redux 连接时发生,如何绕过?对象作为 React 孩子无效

[英]React Error, happens when I use Redux connect in a children, how to bypass?Objects are not valid as a React child

Here is an Error:这是一个错误:

Objects are not valid as a React child (found: object with keys {$$typeof, type, compare, WrappedComponent}).对象作为 React 子对象无效(发现:object 带有键 {$$typeof, type, compare, WrappedComponent})。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

It works just fine if I don't use connect in main.js and using connect in App.js doesn't make an Error, but once I use connect in main.js it throws me this error.如果我不在 main.js 中使用 connect 并且在 App.js 中使用 connect 不会产生错误,它工作得很好,但是一旦我在 main.js 中使用 connect 它就会抛出这个错误。 What do I do wrong?我做错了什么? And I'm using connect same way as in App.js Thank you我正在使用与 App.js 中相同的方式连接 谢谢

Here is sandBox https://codesandbox.io/s/busy-euler-7mpi7?file=/src/main.js这里是沙盒 https://codesandbox.io/s/busy-euler-7mpi7?file=/src/main.js

you can experience, just delete connect in main.js and it will start working您可以体验一下,只需删除 main.js 中的 connect 即可开始工作

App.js应用程序.js

import React, { useEffect } from "react";
import './styles/main.scss';
import './App.scss';

import routes from "./router/router";

import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import { connect } from "react-redux";

import {checkUser, fetchUsers, fetchPolls} from "./store/index";


function App (props) {
    useEffect(() => {
        let { loadUsers} = props
        loadUsers();
    }, [])

    let jsxRoutes = routes.map(el =>
        <Route
            path={el.url}
            exact={el.exact}
            key={el.url}>
            { el.component }
        </Route>
            )
    return (
            <Router>
                <div className="App">
                    <Switch>
                        { jsxRoutes }
                    </Switch>
                </div>
            </Router>
    );
}

const mapStateToProps = state => {
    return {
        users: state.users.data,
    }
}

const mapDispatchToProps = dispatch => {
    return {
        loadUsers: () => dispatch(fetchUsers())
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

Main.js主.js

import React from "react";
import "./main.scss"

import { connect } from "react-redux"


const Main = (props) => {
    return(
        <main>
            main pg
        </main>
    )
}



export default connect(null, null)(Main);

在此处输入图像描述

In App.js try this instead:App.js 中试试这个:

let jsxRoutes = routes.map((el) => (
  <Route path={el.url} exact={el.exact} key={el.url} component={el.component} />
));

Or the shorter version: <Route {...el} />或者更短的版本: <Route {...el} />

暂无
暂无

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

相关问题 我不断收到此错误 Objects are not valid as a React child {} 如果您要呈现子集合,请改用数组 - I keep getting this error Objects are not valid as a React child {} If you meant to render a collection of children, use an array instead 如何修复错误:对象作为 React 子对象无效。 如果您打算渲染一组孩子,请改用数组 - How to fix error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead 对象不是有效的React子级Redux - Objects are not valid as a React child, Redux 尝试响应 MAP 数据时出现错误。 对象作为 React 子对象无效(找到:object 和键 {children}),我该如何解决? - I AM HAVING ERROR WHEN TRYING TO MAP A DATA IN REACT . Objects are not valid as a React child (found: object with keys {children}), How do i solve it? React错误:对象作为React子对象无效,如果要渲染子代集合,请改用数组 - React Error: Objects are not valid as a React child,If you meant to render a collection of children, use an array instead react matrerial ui Error: Objects are not valid as a React child 如果您打算渲染一组子项,请改用数组 - react matrerial ui Error: Objects are not valid as a React child If you meant to render a collection of children, use an array instead 对象作为 React 子级无效。 如果您打算渲染一组子项,请改用数组 - 错误 Solidity - React - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead - Error Solidity - React 对象作为 React 子级无效,渲染一组子级,使用数组代替 - Objects are not valid as a React child, render a collection of children, use an array instead 错误:在非常简单的Redux应用程序中,“对象作为React子对象无效” - Error: 'Objects are not valid as a React child' in a very simple redux app ReactJS 错误:对象作为 React 子项无效。 如果您打算渲染一组孩子,请改用数组 - ReactJS Error: Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM