简体   繁体   English

函数作为 React 子级无效。 如果您返回 Component 而不是<component />从渲染。 在反应 v6

[英]Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. in react v6

Hello I'm new to react and got myself stuck here, my version is v6 and hence having a bit of a problem understanding, I'm following a tutorial from udemy你好,我是新手,我被困在这里,我的版本是 v6,因此理解有点问题,我正在学习 udemy 的教程

The error that occurred is - Functions are not valid as a React child.发生的错误是 - 函数作为 React 子项无效。 This may happen if you return a Component instead of from render.如果您返回一个组件而不是从渲染中返回,则可能会发生这种情况。 Or maybe you meant to call this function rather than return it.或者,也许您打算调用此 function 而不是返回它。

this is in my Home.js这是在我的 Home.js 中

 class Dashboard extends Component { constructor(props) { super(props); this.state = { child: props.nestedRoute, search: "", }; } componentDidMount() { this.activeNav(); } activeNav() { const pathname = window.location.pathname; const possibleRoutes = [ { routes: "/dashboard", targetId: "home" }, { routes: "/addProduct", targetId: "addProduct" }, { routes: "/products", targetId: "products" }, { routes: "/profile", targetId: "profile" }, ]; possibleRoutes.forEach(({ route, targetId }) => { window.jQuery(`#${targetId}`).removeClass("active"); if (route === pathname) { window.jQuery(`#${targetId}`).addClass("active"); } }); } render() { const Child = this.state.child console.log(Child) const { user } = this.props.auth; return ( <div id="content-wrapper" className="d-flex flex-column"> <div id="content"> <nav className="navbar navbar-expand navbar-light bg-white topbar mb-44 static-top shadow"> <ul className="navbar-nav ml-auto"> <li className='nav-item dropdown no-arrow'> <Link className="nav-link dropdown-toggle" to="#" id="userDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" > <span className="mr-2 d-none d-lg-inline text-gray-600 small"> {user.name} </span> <Avatar size={40}> {user.name && this.avatarText(user.name)} </Avatar> </Link> <div className="dropdown-menu dropdown-menu-right shadow animated--grow-in" aria-labelledby="userDropdown" > <Link className="dropdown-item" to="#"> <i className="fas fa-user fa-sm fa-fw mr-2 text-gray-400"></i> Profile </Link> <Link className="dropdown-item" to="#"> <i className="fas fa-cogs fa-sm fa-fw mr-2 text-gray-400"></i> Settings </Link> <Link className="dropdown-item" to="#"> <i className="fas fa-list fa-sm fa-fw mr-2 text-gray-400"></i> Activity Log </Link> <div className="dropdown-divider"></div> <Link className="dropdown-item" to="#" onClick={this.logUserOut} > <i className="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i> Logout </Link> </div> </li> </ul> </nav> <Child {...this.props} search={this.state.search} /> </div> </div> ); } } Dashboard.propTypes = { auth: PropTypes.object.isRequired, logout: PropTypes.func.isRequired, }; const mapStateToProps = (state) => ({ auth: state.auth, }); export default connect(mapStateToProps, { logout })(Dashboard);

my App.js looks like this我的 App.js 看起来像这样

 function App(props) { useEffect(() => { store.dispatch(setCurrentUser()) }, []) return ( <Provider store={store}> <Router> <Routes> <Route exact path="/" element={<Landing/>} /> <Route exact path="/" element={<ProtectedRoute/>}> <Route exact path="/dashboard" element={()=> (<Dashboard {...props} nestedRoute={Home} />)} /> <Route exact path="/dashboard/addProduct" element={()=> (<Dashboard {...props} nestedRoute={AddProduct} />)} /> </Route> <Route exact path="/register" element={<Register/>} /> <Route exact path="/login" element={<Login/>} /> </Routes> </Router> </Provider> ); }

in this line <Child {...this.props} search={this.state.search} /> did you import a child component for this在这一行 <Child {...this.props} search={this.state.search} /> 你是否为此导入了子组件

暂无
暂无

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

相关问题 函数作为 React 子级无效。 如果您返回一个组件而不是从渲染中返回,则可能会发生这种情况。 Redux - Functions are not valid as a React child. This may happen if you return a Component instead of from render. Redux React 显示“函数作为 React 子级无效。 如果您返回 Component 而不是<component />从渲染” - React is showing “Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render” 函数作为 React 子级无效。 如果您返回 Component 而不是<component />从渲染 - Functions not valid as a React child. This may happen if you return a Component instead of <Component /> from render 函数作为 React 子项无效。 如果您返回一个组件而不是从渲染中返回,则可能会发生这种情况 - Functions are not valid as a React child. This may happen if you return a Component instead of from render 函数作为 React 子级无效。 如果您返回 Component 而不是<component />从渲染。 如何解决这个问题 - Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render . how can fix that 错误函数作为 React 子级无效。 如果您返回 Component 而不是<Component />从渲染 - Error Functions are not valid as a React child. This can happen if you return a Component instead of <Component /> from render 警告:函数作为 React 子项无效。如果您返回组件而不是返回组件,则可能会发生这种情况<component />从渲染 - Warning: Functions are not valid as a React child.This may happen if you return a Component instead of <Component /> from render react native: 函数作为 react child 无效。如果你返回一个组件而不是一个组件,这可能会发生<component/> - react native: functions are not valid as a react child .This may happen if you return a component instead of a <component/> 对象作为 React 子项无效。 如果您打算渲染子集合,请改用数组。 如何退货? - Objects are not valid as a React child. If you meant to render a collection of children, use an array instead. How to return it? 编写我的第一个 HOF 反应组件时出错:如果您返回一个组件而不是返回一个组件,则可能会发生这种情况<Component /> - Error writing my first HOF react component: This may happen if you return a return a Component instead of <Component />
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM