admin管理员组文章数量:1024680
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import Home from './ponents/Home';
import About from './ponents/About';
import Skills from './ponents/Skills';
const titles=["About","Projects","Contact","Education","Skills","Resume"];
ReactDOM.render(
<Router>
<Switch>
{
titles.map(title=>{
if(title==="Home"){return false;}
var path=title.toLowerCase();
console.log(title)
return (<Route exact path={"/"+path} ponent={title}/>)
})
}
<Route exact path="/" ponent={Home}/>
<Route ponent={NotFound}/>
</Switch>
</Router>,document.getElementById('root'));
<Route exact path="/" ponent={About}/>//if i put it like this it works(not in this place)
I dont know why it is not working i tried to remove "" after maping array but it didnt work aswell.
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import Home from './ponents/Home';
import About from './ponents/About';
import Skills from './ponents/Skills';
const titles=["About","Projects","Contact","Education","Skills","Resume"];
ReactDOM.render(
<Router>
<Switch>
{
titles.map(title=>{
if(title==="Home"){return false;}
var path=title.toLowerCase();
console.log(title)
return (<Route exact path={"/"+path} ponent={title}/>)
})
}
<Route exact path="/" ponent={Home}/>
<Route ponent={NotFound}/>
</Switch>
</Router>,document.getElementById('root'));
<Route exact path="/" ponent={About}/>//if i put it like this it works(not in this place)
I dont know why it is not working i tried to remove "" after maping array but it didnt work aswell.
Share Improve this question asked Apr 18, 2018 at 19:30 curious ladcurious lad 1642 silver badges14 bronze badges 1- You seem to be using a string as a ponent. You can learn how to do that from here: stackoverflow./questions/29875869/… – Titus Commented Apr 18, 2018 at 19:33
1 Answer
Reset to default 3Because const titles=["About","Projects","Contact","Education","Skills","Resume"];
is just an array of strings you will not be able to use the ponent=
syntax (because these are strings not ponents)... What you could easily do is make titles an array of objects
const titles=[{name: "About", ponent: About...}];
... return (<Route exact path={"/"+title.name} ponent={title.ponent}/>)
Which would allow you to mostly keep what you already have.
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import Home from './ponents/Home';
import About from './ponents/About';
import Skills from './ponents/Skills';
const titles=["About","Projects","Contact","Education","Skills","Resume"];
ReactDOM.render(
<Router>
<Switch>
{
titles.map(title=>{
if(title==="Home"){return false;}
var path=title.toLowerCase();
console.log(title)
return (<Route exact path={"/"+path} ponent={title}/>)
})
}
<Route exact path="/" ponent={Home}/>
<Route ponent={NotFound}/>
</Switch>
</Router>,document.getElementById('root'));
<Route exact path="/" ponent={About}/>//if i put it like this it works(not in this place)
I dont know why it is not working i tried to remove "" after maping array but it didnt work aswell.
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import Home from './ponents/Home';
import About from './ponents/About';
import Skills from './ponents/Skills';
const titles=["About","Projects","Contact","Education","Skills","Resume"];
ReactDOM.render(
<Router>
<Switch>
{
titles.map(title=>{
if(title==="Home"){return false;}
var path=title.toLowerCase();
console.log(title)
return (<Route exact path={"/"+path} ponent={title}/>)
})
}
<Route exact path="/" ponent={Home}/>
<Route ponent={NotFound}/>
</Switch>
</Router>,document.getElementById('root'));
<Route exact path="/" ponent={About}/>//if i put it like this it works(not in this place)
I dont know why it is not working i tried to remove "" after maping array but it didnt work aswell.
Share Improve this question asked Apr 18, 2018 at 19:30 curious ladcurious lad 1642 silver badges14 bronze badges 1- You seem to be using a string as a ponent. You can learn how to do that from here: stackoverflow./questions/29875869/… – Titus Commented Apr 18, 2018 at 19:33
1 Answer
Reset to default 3Because const titles=["About","Projects","Contact","Education","Skills","Resume"];
is just an array of strings you will not be able to use the ponent=
syntax (because these are strings not ponents)... What you could easily do is make titles an array of objects
const titles=[{name: "About", ponent: About...}];
... return (<Route exact path={"/"+title.name} ponent={title.ponent}/>)
Which would allow you to mostly keep what you already have.
本文标签: javascriptReact Router map trough arrayStack Overflow
版权声明:本文标题:javascript - React Router map trough array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745611848a2159046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论