【问题标题】:Button navigation not working properly in react js按钮导航在反应 js 中无法正常工作
【发布时间】:2021-08-29 16:03:53
【问题描述】:

我是 React-JS 的初学者。在这里,我尝试单击按钮并导航到另一个页面。但导航无法正常工作。我正确地将点击功能调用到按钮中。你们能检查我的代码并解决我的问题吗? “Voir toutes les notification”是按钮名称。

import React from 'react'
import Footer from '../../components/Footer/index.js';
import Header from "../../components/Header/index.js"
import Carnetdenote from "../../public/img/Carnetdenote.svg"
import Settings from "../../public/img/AdminSettings.svg"
import Producteurs from "../../public/img/Producteurs.svg"




class consultantdashboard extends React.Component {

    onClick = () => this.props.history.push("/consultantnotification_2.6");

    render() {
        return (

            <div>
                <Header></Header>
                <div>

                    <div>
                        <main>
                            <div className="headerBecground">


                               

                                <div >

                                    <div className="desktophidden">
                                        <div className="producteure-dashboard-table-box">

                                            <h4 className="headerColor">Notifications</h4>

                                            <div class="table-responsive">
                                                <table class="table">

                                                    <tr>
                                                        <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>
                                    &nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Une nouvelle photo à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                    </tr>

                                                    <tr>
                                                        <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>
                                    &nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Un nouveau commentaire à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                    </tr>

                                                    <tr>
                                                        <td>
                                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Une nouvelle photo à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                    </tr>

                                                    <tr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-primary primaryTop" onClick={this.onClick}>Voir toutes les notificationsx</button></tr>



                                                </table>

                                            </div>

                                        </div>
                                    </div>
                                </div>


                                <br></br>


                                <div class=" mobilehidden">

                                    <div class="container-fluid" >

                                        <div class="card-body">
                                            <div className="producteure-dashboard-table-box">

                                                <h4 className="headerColor">Notifications</h4>



                                                <div class="table-responsive">
                                                    <table class="table " width="100%" cellspacing="0">
                                                        <thead>
                                                            <tr>
                                                                <th></th>
                                                                <th>Date</th>
                                                                <th>Activité</th>
                                                                <th>Auteur</th>
                                                            </tr>
                                                        </thead>

                                                        <tr>
                                                            <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg></td>
                                                            <td>2021-03-29</td>
                                                            <td>Une nouvelle photo à été ajouté</td>
                                                            <td>Victor Morin</td>
                                                        </tr>
                                                        <tr>
                                                            <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512" ><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg></td>
                                                            <td>2021-03-29</td>
                                                            <td>Un nouveau commentaire à été ajouté</td>
                                                            <td>Victor Morin</td>
                                                        </tr>
                                                        <tr>
                                                            <td></td>
                                                            <td>2021-03-29</td>
                                                            <td>Une nouvelle photo à été ajouté</td>
                                                            <td>Victor Morin</td>
                                                        </tr>
                                                        <tr>
                                                            <td></td>
                                                            <td></td>
                                                            <td></td>
                                                            <td><button type="button" class="btn btn-primary primaryTop">Voir toutes les notificationsx</button></td>
                                                        </tr>



                                                    </table>

                                                </div>
                                            </div>


                                        </div>
                                    </div>
                                </div>




                                <div className="producteure-dashboard-box">

                                    <div class="row">
                                        <div class="col-md-12">

                                            <div id="grid" class="row">

                                                <div class="col-md-6 col-6">
                                                    <div>
                                                        <div class="producteure-dashboard-grid-item">
                                                            <img src={Producteurs} class="img-responsive" alt="workimg" />
                                                            <p>Producteurs</p>
                                                        </div>
                                                    </div>

                                                </div>
                                                <div class="col-md-6 col-6">
                                                    <div>
                                                        <div class="producteure-dashboard-grid-item">
                                                            <img src={Carnetdenote} class="img-responsive" alt="workimg" />
                                                            <p>Carnet de note</p>
                                                        </div>
                                                    </div>

                                                </div>
                                                <div class="col-md-6 col-6">
                                                    <div>
                                                        <div class="producteure-dashboard-grid-item">
                                                            <img src={Settings} class="img-responsive" alt="workimg" />
                                                            <p>Settings</p>
                                                        </div>
                                                    </div>

                                                </div>

                                            </div>
                                        </div>
                                    </div>

                                </div>











                            </div>



                        </main>

                        <Footer></Footer>
                    </div>
                </div>
            </div>
        )
    }
}

export default consultantdashboard

【问题讨论】:

  • 尝试将按钮的 onClick 包裹在 ES6 箭头函数中 - 即 () => this.onClick()
  • navigation is not working properly。你能详细说明错误或行为吗?
  • 嘿 @Incorrect syntax near 你能举个例子吗
  • 是的,为类似问题提供了一个可行的解决方案:stackoverflow.com/questions/45246933/…
  • @问题附近的语法不正确是我想用另一个页面导航我该怎么做。你能改变我的代码并给出答案吗?

标签: javascript reactjs react-router jsx


【解决方案1】:

您可以使用 react-router-dom 的 &lt;Redirect to="{your path}" /&gt; 重定向到另一个页面。您可能无法在道具中获取历史记录,因此您无法重定向。

import React from 'react';
import { Redirect } from 'react-router-dom';
import Footer from '../../components/Footer/index.js';
import Header from "../../components/Header/index.js"
import Carnetdenote from "../../public/img/Carnetdenote.svg"
import Settings from "../../public/img/AdminSettings.svg"
import Producteurs from "../../public/img/Producteurs.svg"

class consultantdashboard extends React.Component {

   constructor(props) {
       super(props);
       this.state = {
           redirect: null
       };
   }

  onClick = () => this.setState({ redirect: "/consultantnotification_2.6" });

  render() {
    if (this.state.redirect) {
        return <Redirect to={this.state.redirect} />
    }

    return (

        <div>
            <Header></Header>
            <div>

                <div>
                    <main>
                        <div className="headerBecground">


                           

                            <div >

                                <div className="desktophidden">
                                    <div className="producteure-dashboard-table-box">

                                        <h4 className="headerColor">Notifications</h4>

                                        <div class="table-responsive">
                                            <table class="table">

                                                <tr>
                                                    <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>
                                &nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Une nouvelle photo à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                </tr>

                                                <tr>
                                                    <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg>
                                &nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Un nouveau commentaire à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                </tr>

                                                <tr>
                                                    <td>
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2021-03-29<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Une nouvelle photo à été ajouté<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Victor Morin</td>
                                                </tr>

                                                <tr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-primary primaryTop" onClick={this.onClick}>Voir toutes les notificationsx</button></tr>



                                            </table>

                                        </div>

                                    </div>
                                </div>
                            </div>


                            <br></br>


                            <div class=" mobilehidden">

                                <div class="container-fluid" >

                                    <div class="card-body">
                                        <div className="producteure-dashboard-table-box">

                                            <h4 className="headerColor">Notifications</h4>



                                            <div class="table-responsive">
                                                <table class="table " width="100%" cellspacing="0">
                                                    <thead>
                                                        <tr>
                                                            <th></th>
                                                            <th>Date</th>
                                                            <th>Activité</th>
                                                            <th>Auteur</th>
                                                        </tr>
                                                    </thead>

                                                    <tr>
                                                        <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512"><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg></td>
                                                        <td>2021-03-29</td>
                                                        <td>Une nouvelle photo à été ajouté</td>
                                                        <td>Victor Morin</td>
                                                    </tr>
                                                    <tr>
                                                        <td><svg class="svg-inline--fa fa-circle fa-w-16 fontColorRed" viewBox="0 0 512 512" ><path fill="currentColor" d="M256 8C119 8 8 119 8 256s111 248 248 248 248-111 248-248S393 8 256 8z"></path></svg></td>
                                                        <td>2021-03-29</td>
                                                        <td>Un nouveau commentaire à été ajouté</td>
                                                        <td>Victor Morin</td>
                                                    </tr>
                                                    <tr>
                                                        <td></td>
                                                        <td>2021-03-29</td>
                                                        <td>Une nouvelle photo à été ajouté</td>
                                                        <td>Victor Morin</td>
                                                    </tr>
                                                    <tr>
                                                        <td></td>
                                                        <td></td>
                                                        <td></td>
                                                        <td><button type="button" class="btn btn-primary primaryTop">Voir toutes les notificationsx</button></td>
                                                    </tr>



                                                </table>

                                            </div>
                                        </div>


                                    </div>
                                </div>
                            </div>




                            <div className="producteure-dashboard-box">

                                <div class="row">
                                    <div class="col-md-12">

                                        <div id="grid" class="row">

                                            <div class="col-md-6 col-6">
                                                <div>
                                                    <div class="producteure-dashboard-grid-item">
                                                        <img src={Producteurs} class="img-responsive" alt="workimg" />
                                                        <p>Producteurs</p>
                                                    </div>
                                                </div>

                                            </div>
                                            <div class="col-md-6 col-6">
                                                <div>
                                                    <div class="producteure-dashboard-grid-item">
                                                        <img src={Carnetdenote} class="img-responsive" alt="workimg" />
                                                        <p>Carnet de note</p>
                                                    </div>
                                                </div>

                                            </div>
                                            <div class="col-md-6 col-6">
                                                <div>
                                                    <div class="producteure-dashboard-grid-item">
                                                        <img src={Settings} class="img-responsive" alt="workimg" />
                                                        <p>Settings</p>
                                                    </div>
                                                </div>

                                            </div>

                                        </div>
                                    </div>
                                </div>

                            </div>











                        </div>



                    </main>

                    <Footer></Footer>
                </div>
            </div>
        </div>
    )
  }
}

export default consultantdashboard

【讨论】:

  • 感谢您回答这个问题。但是导航不起作用。
【解决方案2】:

最简单的方法是使用 withRouter 高阶组件包装您的组件,该组件将向您的组件添加历史道具。

import React from "react";
import { withRouter } from "react-router-dom";
...

class consultantdashboard extends React.Component {
    onClick = () => this.props.history.push("/consultantnotification_2.6");
    render (
        ...
    );
}

export default withRouter(consultantdashboard);

【讨论】:

    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2017-10-06
    • 2018-12-09
    • 1970-01-01
    • 2020-07-25
    • 2019-08-14
    相关资源
    最近更新 更多