Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a new component on button click in ReactJs?

I created a main component in ReactJs called MainPage (using 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);

I want to load a new component (either CompA or CompB, depending on which button was clicked - ButtonA or ButtonB) on button click. A new component should completely replace a current component - I mean that it should be loaded in a whole screen (not anywhere next to buttons).

How can I do it?

UPDATE:

I want to replace MainPage component, not just render on top of it.

This is how I load 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'));
like image 253
ScalaBoy Avatar asked Aug 31 '25 00:08

ScalaBoy


1 Answers

You can create a different component to handle the state and add an if statement in that component to handle the view that you want to render.

You can see the example here codesandbox.io/embed/6wx2rzjrr3

App.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);

Main.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>{" "}
  </>
);

View1.js

import React from "react";

export default props => "View 1";

View2.js

import React from "react";

export default props => "View 2";
like image 77
Meir Keller Avatar answered Sep 02 '25 22:09

Meir Keller