简体   繁体   中英

Component is not getting rendered using router-link in React.js

Code snippet from Navigation.js

class Navigation extends Component { render() { return (

    <Router>
 <nav className="navbar navbar-expand-lg navbar-light bg-light">
          <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
    </button>

    <div className="collapse navbar-collapse" id="navbarSupportedContent">
      <ul className="navbar-nav mr-auto">

          <NavDropdown name="Test">
            <a className="dropdown-item" >

                <li><Link to={"/Invoice"} >Mexico Invoice</Link></li>

                   </a>
          </NavDropdown>

           <NavDropdown name="Analysis">
            <a className="dropdown-item" href="/Transaction">Audit Log</a>
        </NavDropdown>
      </ul>

   <Route exact path="/Invoice" component={Invoice}  />
             </div>
           </nav>
            </Router>
);

} }

export default withRouter(Navigation)

I am rendering this component from App.js

class App extends Component {
  constructor(props) {
    super(props);

  }

  render() {
    return (
      <Router>
      <div >
        <HeaderAmex className="App" />
        <div>
   <Navigation/>
   </div>
   <Route exact path="/invoice" component={Invoice} /> 
         </div>
         </Router>
    );
  }
}

export default withRouter(App)

Below is my Invoice.js

class Invoice extends Component {
  constructor(props) {
    super(props);
    this.state = { items: [], isLoaded: false, transId: "" ,flag:true,errorFlag:false,show:false,displayContent:"",invoice:"",invoiceXmlBody:"",invoiceTransId:"",invoiceResultFlag:false,invoicedisplayFlag:false};

  }



  handleChangeInvoice(e) {
    console.log("inside handleChangeInvoice");
    let invoiceXml = e.target.value;
    if (invoiceXml !== undefined) {
      this.setState({ invoiceXmlBody: e.target.value });
    }
  }

  handleSubmitInvoiceXml =e=>{
   console.log("*******************inside handleSubmitInvoiceXml***************");
   let url = "xxxxxx";
   fetch(url, {
    method: "POST",
    body: JSON.stringify(this.state.invoiceXmlBody),
    headers: {
      "Content-Type": "application/xml"
    },
    credentials: "xxxxx"
  }).then(res => res.json())
  .then(json => {
    this.setState({
      invoiceTransId:json,

    });
    if(json===undefined){
this.state.invoiceResultFlag=true;
    }
    else{

      this.state.invoicedisplayFlag=true;
      console.log("inside=========else===="+this.state.invoicedisplayFlag);
    }
    }
 ) }


  render() {
    const { match, location, history } = this.props
    console.log("---------------------------"+this.state.invoicedisplayFlag);
    return (

             <div className="App">
                <div>{location.pathname}</div>
        <br/>

          <label className="lable" style={{marginRight:19}}>
              InvoiceXml:
              <input
                type="text"
                name="invoiceXml" placeholder="Enter invoice xml" 
                onBlur={this.handleChangeInvoice.bind(this)}  style={{marginLeft:15}}
              />

            </label><br/><br/>


             <input type="button" onClick={this.handleSubmitInvoiceXml} name="Submit" value="Submit" className="submitButton"  style={{marginLeft:-60}}/>


            <br/>
            <br/><br/>



            <div  className={this.state.invoicedisplayFlag?"showDisplay":"hide"}>
            <h5>Transaction Id is :{this.state.invoiceTransId}</h5>



        </div>


      </div>
    );
  }
}

export default withRouter(Invoice)

and below is my index.js

 ReactDOM.render(<App />, document.getElementById('root'));

 serviceWorker.unregister();

Can anyone please help me to display the invoice component.I am pretty new to React.

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

<Router>
   <div>
     <Route exact path="/" component={Home}/>
     <Route path="/news" component={NewsFeed}/>
   </div>
</Router>

That is the example code of react-router-dom . You use the Link component which uses the routes that you should have defined before. I think you are also missing some setup. You should wrap your app inside the router.

I hope this helps you. But it seems that you did not understand react-router-dom. Reading the docs will help you.

You're getting this error because Navigation component is outside the main Router and component path should be path={''} not paths={''}

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

 class App extends Component {
 constructor(props) {
  super(props);
 }

 render() {
   return (
   <Router>
    <div >
        <HeaderAmex className="App" />
        <div>
           <Navigation/>
        </div>

        <Route exact path="/" render={()=><h1>main<h1 />} /> 

        <Route path="/invoice" component={Invoice} /> 
    </div>
   </Router>    
   );
 }
 }

export default App;

in order to access the location props in your component you have to warp Invoice component with withRouter(Invoice)

How to pass the React Router location prop to a component?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM