函数作为 React 子项无效。如果您 return 组件而不是渲染中的 <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
你好,我是 React 的新手,我被困在这里,我的版本是 v6,因此理解有点问题,我正在学习 udemy 的教程
发生的错误是 - 函数作为 React 子项无效。如果您 return 一个组件而不是来自渲染,则可能会发生这种情况。或者您可能打算调用此函数而不是 return 它。
这是我的 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);
我的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>
);
}
在你的 React 路由器中使用 render method。
<Route exact
path="/dashboard/addProduct"
render={()=> <Dashboard {...props} nestedRoute={AddProduct} />}
/>
你好,我是 React 的新手,我被困在这里,我的版本是 v6,因此理解有点问题,我正在学习 udemy 的教程
发生的错误是 - 函数作为 React 子项无效。如果您 return 一个组件而不是来自渲染,则可能会发生这种情况。或者您可能打算调用此函数而不是 return 它。
这是我的 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);
我的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>
);
}
在你的 React 路由器中使用 render method。
<Route exact
path="/dashboard/addProduct"
render={()=> <Dashboard {...props} nestedRoute={AddProduct} />}
/>