1
    import React from 'react';
    import ReactDOM from 'react-dom';
    import { BrowserRouter  as Router, Switch, Route  } from "react-router-dom";   import Home from './components/Home';
    import About from './components/About';
    import Skills from './components/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} component={title}/>)
              })
            }
              <Route exact path="/" component={Home}/>
              <Route component={NotFound}/>
            </Switch>
      </Router>,document.getElementById('root'));
<Route exact path="/" component={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.

1

1 Answer 1

3

Because const titles=["About","Projects","Contact","Education","Skills","Resume"]; is just an array of strings you will not be able to use the component=syntax (because these are strings not components)... What you could easily do is make titles an array of objects

const titles=[{name: "About", component: About...}];

... return (<Route exact path={"/"+title.name} component={title.component}/>)

Which would allow you to mostly keep what you already have.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.