2

I am trying to use nested routes with react router but its not working for me. I also tried to add nested router on main App but it's not working.

App.js

import Message from './containers/Message'
import {connect} from 'react-redux'
import {addMessage, setMessages} from './store/actions/message'

import {
    BrowserRouter as Router,
    Route
} from "react-router-dom";

class App extends Component {

    constructor(props) {
        super(props)
        WebSocketInstance.addCallbacks(
            this.props.setMessagesMethod.bind(this),
            this.props.addMessageMethod.bind(this)
        )
    }

    render() {
        return (
            <Router>
                <Route exact path='/login/' component={WrappedLoginForm} />
                <Route exact path='/' component={ChatApp} />
                {/* <Route path='/:chatID' component={Message} /> */}
            </Router>
        )
    }
}

ChatApp.js

export class ChatApp extends Component {

  componentWillMount() {
    if (!this.props.loading){
      this.props.autoLogin(this.props.history)
    }
  }

  render() {
    return (
      this.props.isAuthenticated ?
      (<div id='frame' >
        <Sidepanel />
        <div className="content">
          <div className="contact-profile">
            <img src="https://www.netfort.com/assets/user.png" alt="" />
            <p>{this.props.user.username}</p>
          </div>

          <Route path='/chat/:chatID' component={Message} />

        </div>
      </div>): null
    )
  }
}

i am trying to match http://localhost:3000/chat/1 but Message component is not rendering.

1 Answer 1

1

If you use exact path any sub routes will not be hit because the sub routes will not match the condition that hits the parent route.

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.