反应:通过道具不起作用。我想念什么吗?
正如您从标题中获得的那样,React中的通过道具不起作用。我不明白。
import styled from 'styled-components';
const Login = (props) => {
return<div>Login</div>
}
export default Login;
在我的app.js页面上,这里有以下内容:
import './App.css';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Login from './components/Login.js';
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route exact path="/" >
<Login />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
问题:如果我启动脚本并渲染页面,则不会显示任何内容。我在做什么错?
as you may get from the title, passing props in react is not working. And i don´t get why.
import styled from 'styled-components';
const Login = (props) => {
return<div>Login</div>
}
export default Login;
On my App.js page here is the following:
import './App.css';
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Login from './components/Login.js';
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route exact path="/" >
<Login />
</Route>
</Routes>
</Router>
</div>
);
}
export default App;
Problem: if i start the script and render the page, nothing is shown. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将
&lt; login /&gt; < /code>作为元素传递。尝试此代码:
app.js:
You should pass
<Login />
as the element. Try this code:App.js:
&lt; login/&gt;
...登录
组件未传递任何道具,因此我不希望在组件中看到任何道具。您的问题是误解路由
组件的工作方式。路由
组件的唯一有效的孩子是另一个路由
在筑巢路由的情况下,否则,route> Route
将路由内容传递给路由内容。组件的元素
prop作为React node
,又称JSX。route
登录
组件应传递给元素
prop。有关更多详细信息/说明,请参见为什么
&lt; route&gt;
有一个元素
prop而不是渲染
或component
?<Login />
... theLogin
component isn't passed any props so I wouldn't expect to see any in the component. Your issue is a misunderstanding how theRoute
component works. The only valid children of theRoute
component is anotherRoute
component in the case of nesting routes, otherwise, the routed content is passed on theRoute
component'selement
prop as aReactNode
, a.k.a. JSX.Route
The
Login
component should be passed to theelement
prop.For more details/explanation see Why does
<Route>
have anelement
prop instead ofrender
orcomponent
?