【问题标题】:ReactJS OnClick renderReactJS OnClick 渲染
【发布时间】:2017-12-20 15:37:58
【问题描述】:

我是 ReactJS 的新手,我正在尝试制作一个菜单,通过按下任何功能,活动类将消失并出现新页面。例如,在这里,我试图点击我的订单并转发到我要求加载的页面。我该如何正确地做到这一点?这是我当前的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import "./index.css";

class MainPanel extends React.Component {
    render() {
        return (
            <div className="main-layout">
                <header>
                    <ul className="top">
                        <h1>Header</h1>
                    </ul>
                </header>
                <ul className="categories">
                    <li>Main Panel</li>
                    <li onClick={<MyOrder />}>My Order</li>
                    <li>Technical Support</li>
                    <li>My Payments</li>
                    <li>Suggestions/ Questions</li>
                </ul>
            </div>
        );
    }
}
function MyOrder () {
        return (
            <div className="main-layout">
                <header>
                    <ul className="top">
                        <h1>My Order</h1>
                    </ul>
                </header>
                <ul className="categories">
                    <li>Where is my order?</li>
                    <li>My order delays more than the expected time</li>
                    <li>My order status shows that the order arrived but it did not</li>
                    <li>I have a complaint</li>
                    <li>Suggestions/ Questions</li>
                </ul>
            </div>
        );
}

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

【问题讨论】:

  • 你可能想看看 React Router。
  • 嘿,我读过 React Router,但我不想在更改页面时更改 URL,只是渲染页面,这样用户基本上在同一页面上并看到不同的东西。

标签: reactjs


【解决方案1】:

如果没有 React Router 和其他 3rd 方库,您可以执行以下操作:

class Overview extends React.Component {
    onMenuClick(id) {
        return this.props.onMenuClick(id);
    }

    render() {
        return(
            <ul className="categories">
                <li onClick={this.onMenuClick.bind(this, "mainPanel")}>Main Panel</li>
                <li onClick={this.onMenuClick.bind(this, "myOrder")}>My Order</li>
                <li onClick={this.onMenuClick.bind(this, "technicalSupport")}>Technical Support</li>
                <li onClick={this.onMenuClick.bind(this, "myPayments")}>My Payments</li>
                <li onClick={this.onMenuClick.bind(this, "suggestions")}>Suggestions/ Questions</li>
            </ul>
        );
    }
}

class MainPanel extends React.Component {
    // sets the initial value for this.state.page
    componentWillMount() {
        this.setState({
            page: "overview"
        });
    }

    switchPage(id) {
        this.setState({ page: id });
    }

    showComponent() {
        if(this.state.page === "overview") return (<Overview
            onMenuClick={::this.switchPage}
        />);
        if(this.state.page === "myOrder") return <MyOrder />;

        throw new Error(`${this.state.page} is not a valid page id`);
    }

    render() {
        return (
            <div className="main-layout">
                <header>
                    <ul className="top">
                        <h1>Header</h1>
                    </ul>
                </header>
                { this.showComponent() }
            </div>
        );
    }
}

您通过更新状态来更改视图。根据状态不同的组件被安装/卸载。

根据我的经验,请注意:尝试通过 React Baobab 或类似的方式(中心化状态)处理您的应用状态(在我们的例子中为当前页面信息),否则所有这些道具冒泡会变得混乱。

【讨论】:

  • 嘿,非常感谢您的帮助。我的代码现在导致一个空白页,我该怎么办?
  • 也许你应该在 showComponent 的末尾抛出一个错误: throw new Error(${this.state.page} is not a valid page id);
  • 我认为您没有 this.state.page 的初始值。要设置它,请使用 componentWillMount() { this.setState({ page: "overview" }) }
  • 嘿,它现在可以加载,但在单击任何类别时都无法正常工作,我收到错误消息。 TypeError: this.setState 不是函数
  • 当 this 不指向 MainPanel,而是指向其他任何地方时,就会发生这种情况。您是否使用了 :: 符号?这里有必要
猜你喜欢
  • 1970-01-01
  • 2017-05-16
  • 2018-07-05
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多