簡體   English   中英

沒有使用React.js中的router-link渲染組件

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

Navigation.js中的代碼段

類Navigation擴展了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>
);

}}

使用Router導出默認值(導航)

我正在從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>
    );
  }
}

使用Router(App)導出默認值

以下是我的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)

下面是我的index.js

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

 serviceWorker.unregister();

誰能幫我顯示發票組件嗎?我對React很陌生。

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

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

那是react-router-dom的示例代碼。 您可以使用鏈接組件,該組件使用您之前應該定義的路由。 我認為您還缺少一些設置。 您應該將應用包裝在路由器中。

我希望這可以幫助你。 但似乎您不了解react-router-dom。 閱讀文檔將對您有所幫助。

您收到此錯誤是因為Navigation組件位於主路由器之外,並且組件路徑應為path={''}而不是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;

為了訪問組件中的位置道具,您必須使用withRouter(Invoice)對Invoice組件進行withRouter(Invoice)

如何將React Router位置道具傳遞給組件?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM