【问题标题】:Can I use a menu or buttons to switch react components on a single page app?我可以使用菜单或按钮在单页应用程序上切换反应组件吗?
【发布时间】:2016-10-11 21:02:13
【问题描述】:

这在某种程度上是this thread 的延续,因为我找到了一种更好的方式来表达我的问题。

我想要做的就是按下侧面菜单上的一个按钮,这将在屏幕的另一部分切换一个反应组件。目前我的应用程序包含嵌套路由,但我不知道如何从嵌套菜单更新组件。

这是我正在尝试做的画:

基本上,在我单击的每个选项中,我都希望在 <HelpPanel/> 部分呈现不同的组件,或者至少需要完全不同的信息(我有不同的图表、图像等需要显示)。

最初我试图通过嵌套路由来完成它,但达到了我被卡住的地步,不知道如何根据用户单击的项目更新我的<HelpPanel/> 组件。这是当前的相关代码:

//app.js
ReactDOM.render(

<Router history={hashHistory}>
<Route path="/" component={App} handler={App}>
    <IndexRoute component={Index}/>
    <Route path="about" component={About}>
    </Route>
    <Route path="help" component={Help}>
        <Route path="help/:helpOption" component={HelpPanel}/>

    </Route>

</Route>
</Router>,
destination
);

//help.js

导出类 HelpPanel 扩展组件{

render(){

        return (

            <div>{this.props.params.helpOption}</div>
        );
}

}

导出类帮助扩展组件{

渲染(){

   return (
       <Grid>
            <Title/>
           <Row className="show-grid">
               <Col lg={2} md={4} sm={4}>
                    <HelpMenu/>
               </Col>
               <Col lg={8} md={8} sm={8}>
                    {this.props.children || "Please select topic from the menu for more information"}
               </Col>

           </Row>
       </Grid>
   );

} }

【问题讨论】:

  • 我是 React 的新手,但我一直在使用 Redux。你会在收听消息的内容中做什么,然后你可以更新它的状态,这反过来会当然重绘它的内容。然后你需要做的就是从按钮发送一条消息来设置你想要的状态。例如 Action.setView('option1');
  • @Keith 我希望有一种方法可以在避免 Redux 的同时做到这一点,但我越来越觉得我无法逃脱它了。感谢您的建议!

标签: javascript reactjs react-router single-page-application


【解决方案1】:

我已经为这个任务构建了解决方案。不完美,但它有效。
看看http://codepen.io/dagman/pen/gwzBjX。
所以当你点击option2&lt;HelpPanel2 /&gt;替代&lt;HelpPanel1 /&gt;等等。 Afaik react-router 是关于在指定的 url 路径匹配时加载组件。

    class HelpPage extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                panelIndex: 0
            };
        }

    getBtnId = (e) => {
        if(e.target && e.target.nodeName == "BUTTON") {
            console.log(e.target);
            this.setState({
                panelIndex: Number(e.target.id)
            });
      }
    };

    render() {
        return (
            <div className="container">
                <HelpMenu
                    getBtnId={this.getBtnId}
                />
                <HelpPanels
                    panelIndex={this.state.panelIndex}
                />
            </div>
        )
    }
}

const HelpMenu = ({ getBtnId }) => {
    return (
        <div 
            className="btnGroup"
            onClick={getBtnId}
        >
            <button id="0">Option 1</button>
            <button id="1">Option 2</button>
            <button id="2">Option 3</button>
            <button id="3">Option 4</button>
        </div>
    );
};

class HelpPanels extends React.Component {

    render() {
        const panels = [
            <HelpPanel1 />,
            <HelpPanel2 />,
            <HelpPanel3 />,
            <HelpPanel4 />
        ];
        const correctPanel = panels[this.props.panelIndex];
        return (
            <div className="panel-box">
            {correctPanel}
            </div>
        );
    }
}

const HelpPanel1 = () => (
    <h1>Panel for Option 1</h1>
);
const HelpPanel2 = () => (
    <h1>Panel for Option 2</h1>
);
const HelpPanel3 = () => (
    <h1>Panel for Option 3</h1>
);
const HelpPanel4 = () => (
    <h1>Panel for Option 4</h1>
);

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

【讨论】:

  • 谢谢!这正是我需要的!不幸的是,当我将它合并到我的应用程序中时,该死的箭头功能不起作用。哦,好吧,我想这是另一个线程的问题。
  • 实际上这是一个非常简单和幼稚的解决方案。了解如何实现目标就足够了。我的意思是,如果构建真正的应用程序,请不要完全依赖此解决方案,因为您将需要根据您的项目需求添加一些调整和增强功能。
  • 哦,是的,这是我需要的临时物品,所以它工作得很好,最终我会完全改变它:)
猜你喜欢
  • 2017-05-24
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多