4

I've tried everything but fail to render component when URL changes. No error messages nothing, react-redux is installed but not using it yet, so it can't be the problem. When I check it from to Google chrome React dev tools, nothing happens, there is no match, history vs anything. I couldn't find a solution, is there any way to make it work?

https://codesandbox.io/s/vm3x9n4k67

Here is my NavBar.js. I import NavLink from react-router-dom and implement these

import React from 'react'
import classes from "./NavBar.css";
import { NavLink } from 'react-router-dom';


const NavBar = (props) => {
    return (
    <div className={classes.NavBar}>
        <h1 className={classes.NavBar_list} >foodbase</h1>
        <ul className={classes.NavBar_list}>

            <NavLink to="/auth"> <li className={classes.NavBar_list_item}>Signin</li></NavLink>
            <NavLink to="/"><li className={classes.NavBar_list_item}>Main Page</li></NavLink>

        </ul>
    </div>
  )
}

export default NavBar

this is my Layout.js render property:

 render() {
          let recipes = [];
          if (this.state.recipes.length > 0) {
          recipes = this.state.recipes;
    }

    return (

        <div>
            <NavBar/>
            <SearchBox
                onChangeHandler={this.onChangeHandler}
                value={this.state.inputValue}
                onSearchClick={this.onClickedHandler} />
            <section className={classes.SearchSection}>
                <ul className={classes.SearchResultArea}>
                    <SearchResults
                        Results={recipes}
                    ></SearchResults>
                </ul>
            </section>

        </div>
    )
}

and finally app.js:

import React, { Component } from 'react';
import { Switch, Route, BrowserRouter, withRouter } from 'react-router-dom';
import Auth from './components/Auth/Auth';
import SearchBox from './components/SearchBox/SearchBox';
import Layout from './containers/Layout/Layout';
import './App.css';



class App extends Component {
  render() {

  return (
  <div>
    <BrowserRouter>
      <Switch>
        <Layout>
          <Route path="/auth" component={Auth} />
          <Route path="/" exact component={SearchBox} />
        </Layout>
      </Switch>
    </BrowserRouter>
    </div>
    );
  }
}

export default withRouter(App);

when clicked Signin

Main Page

1 Answer 1

4

I assume that you need to put your Route components directly into Switch and don't forget to render children in Layout. So try this:

app.js:

class App extends Component {
  render() {

  return (
  <div>
    <BrowserRouter>
      <Layout>
        <Switch>
          <Route path="/auth" component={Auth} />
          <Route path="/" exact component={SearchBox} />
        </Switch>
      </Layout>
    </BrowserRouter>
    </div>
    );
  }
}

Layout.js

render() {
  // ...

  return (
    <div>
      <NavBar />
      { this.props.children } // <-- your route specific components
    </div>
  )
}
Sign up to request clarification or add additional context in comments.

2 Comments

It is still not working but as you said it might be something about props.children, but how
Besides the changes proposed by @streletss, you need to return in your Auth.js component: return form; because right now you are returning an object instead the HTML you are building up.

Your Answer

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