如何在ReactJs中的按钮单击上加载新组件?

时间:2019-01-14 20:23:41

标签: javascript reactjs material-ui

我在ReactJs中创建了一个名为MainPage的主要组件(使用Material-UI)。

import React from 'react';
import Grid from '@material-ui/core/Grid';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent';
import withStyles from '@material-ui/core/styles/withStyles';

const styles = theme => ({
    card: {
        minWidth: 350,
    },
    button: {
        fontSize: '12px',
        margin: theme.spacing.unit,
        minWidth: 350
    },
    extendedIcon: {
        marginRight: theme.spacing.unit,
    }
});

class MainPage extends React.Component {
    constructor() {
        super();
    }

    render() {
        const {
            classes
        } = this.props;

        return ( <
            React.Fragment >
            <
            CssBaseline / >
            <
            Grid container spacing = {
                0
            }
            direction = "column"
            alignItems = "center"
            justify = "center"
            style = {
                {
                    minHeight: '100vh'
                }
            } >
            <
            form onSubmit = {
                this.handleSubmit
            } >
            <
            Card className = {
                classes.card
            } >
            <
            CardContent >
            <
            Grid item xs = {
                3
            } >
            <
            Button variant = "contained"
            size = "medium"
            color = "primary"
            className = {
                classes.button
            }
            type = "submit"
            value = "single" >
            ButtonA <
            /Button> <
            /Grid> <
            Grid item xs = {
                3
            } >
            <
            Button variant = "contained"
            size = "medium"
            color = "primary"
            className = {
                classes.button
            }
            type = "submit"
            value = "batch" >
            ButtonB <
            /Button> <
            /Grid> <
            /CardContent> <
            /Card> <
            /form> <
            /Grid> <
            /React.Fragment>
        );
    }
}

export default withStyles(styles)(MainPage);

我想在单击按钮时加载一个新组件(CompACompB,具体取决于单击哪个按钮-ButtonA或ButtonB)。一个新组件应该完全替换当前组件-我的意思是应该将其加载到整个屏幕中(而不是按钮旁边的任何位置)。

我该怎么办?

更新:

我想替换MainPage组件,而不仅仅是在其顶部进行渲染。

这就是我加载MainPage的方式:

index.js

import React from 'react';
import { render } from 'react-dom';
import MainPage from './components/MainPage';

const View = () => (
  <div>
    <MainPage/>
  </div>
);

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

2 个答案:

答案 0 :(得分:3)

您可以创建其他组件来处理状态,并在该组件中添加if语句来处理要呈现的视图。

您可以在此处codesandbox.io/embed/6wx2rzjrr3

看到示例

App.js

-O

Main.js

import React, { Component } from "react";
import ReactDOM from "react-dom";
import Main from "./Main";
import View1 from "./View1";
import View2 from "./View2";

import "./styles.css";

class App extends Component {
  state = {
    renderView: 0
  };

  clickBtn = e => {
    this.setState({
      renderView: +e.target.value
    });
  };

  render() {
    switch (this.state.renderView) {
      case 1:
        return <View1 />;
      case 2:
        return <View2 />;
      default:
        return <Main clickBtn={this.clickBtn} />;
    }
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

View1.js

import React from "react";

export default props => (
  <>
    Main view{" "}
    <button value={1} onClick={props.clickBtn}>
      View 1
    </button>{" "}
    <button value={2} onClick={props.clickBtn}>
      View 2
    </button>{" "}
  </>
);

View2.js

import React from "react";

export default props => "View 1";

答案 1 :(得分:1)

您可以在index.js中使用

const View = () => (
  <div>
    <MainPage condition='something' />
  </div>
);

然后进入您的主页:

class MainPage extends React.Component {
   constructor() {
      super();
   }

  myCondition1() {
    return (
        <Component1 />
    );
 }


  myCondition2() {
    return (
        <Component2 />
    );
 }

  render() {
     const { condition} = this.props;
     return (
         {condition === 'something' ? this.myCondition1() : this.myCondition2()}
     )
  }
}

更新

这是一个带有简单按钮的示例:

class Condition1 extends React.Component {
  render() {
    return (
        <div>Condition 1</div>
    );
  }
}

class Condition2 extends React.Component {
  render() {
    return (
        <div>Condition 2</div>
    );
  }
}

class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
          condition: true
        };
        this.handleClick = this.handleClick.bind(this);
    }
    
    handleClick(condition) {
      this.setState( {condition} )
    }

  render() {
     const { condition } = this.state;
     return (
         <div>
             <button onClick={() => this.handleClick(true)}>Condition1</button>
             <button onClick={() => this.handleClick(false)}>Condition2</button>
             {condition === true ? <Condition1 /> : <Condition2 />}
         </div>
     )
  }
}

ReactDOM.render( < App / > ,
  document.getElementById('root')
);
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script><div id="root" />