javascript - React Router map trough array - Stack Overflow

import React from 'react';import ReactDOM from 'react-dom';import { BrowserRoutera

    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
Add a ment  | 

1 Answer 1

Reset to default 3

Because 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611849a4635991.html

相关推荐

  • javascript - React Router map trough array - Stack Overflow

    import React from 'react';import ReactDOM from 'react-dom';import { BrowserRoutera

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信