Button Onclick Show Click Again Remove Component React
-
- Updated date Sep 29, 2020
- 60.4k
- 3
In this article, y'all will acquire how to show and hibernate component in react application.
Introduction
In this commodity, nosotros will learn how to hide or prove components in a React applications. ReactJS is an open-source JavaScript library which is used for creating user interfaces. Information technology is developed and maintained by Facebook. Learn more than almost React.
Prerequisites
- We should have the basic knowledge of React.js
- Visual Studio Code IDE should exist installed on your system.
- Bootstrap
Create React.js Projection
To create a new React.js project, open up the command prompt and enter the following command.
- npx create-react-app reduxapp
Open the newly created projection in Visual Studio Code and install Bootstrap in this projection by using the following control.
- npm install --salve bootstrap
Now, open the index.js file and add together import Bootstrap.
- import 'bootstrap/dist/css/bootstrap.min.css' ;
At present get to the src folder and create two new components, named
- Toggle.js
- Kid.js
At present open Toggle.js file add the post-obit code in this component.
- import React, { Component } from 'react'
- consignclass Toggle extends Component {
- constructor(props) {
- super(props);
- this .state = {
- open up:faux ,
- };
- this .togglebutton = this .togglebutton.bind( this );
- }
- togglebutton() {
- const { open } = this .country;
- this .setState({
- open: !open up,
- });
- }
- render() {
- var { title, children } =this .props;
- const { open } = this .state;
- if (open) {
- title ='Hibernate Component' ;
- }else {
- title ='Show Component' ;
- }
- return (
- <div className="container" >
- <divclass = "row" className= "hdr" >
- <divgrade = "col-sm-12 btn btn-info" >
- Show Hibernate component on Click in React JS App
- </div>
- </div>
- <div style={{"marginTop" : "10px" }}>
- <divcourse = "col-sm-8 btn btn-success" onClick={ this .togglebutton}>
- {championship}
- </div>
- {open && (
- <div>
- {children}
- </div>
- )}
- </div>
- </div>
- );
- }
- }
- exportdefault Toggle
At present open Child.js file and add the following code.
- import React, { Component } from "react" ;
- class Child extends Component {
- render() {
- return (
- <div>Kid Component </div>
- );
- }
- }
- consigndefault Kid;
Add a reference of this component in app.js file,
- import React from 'react' ;
- import'./App.css' ;
- import Toggle from"./Toggle" ;
- import Child from"./Child" ;
- function App() {
- return (
- <div className="App" >
- <Toggle title="Show Kid" >
- <Child />
- </Toggle>
- </div>
- );
- }
- exportdefault App;
Now run the project by using 'npm first' and cheque the issue.
Now click on the bear witness component button.
Now go to src binder and create 2 different components named Comp1.js, Comp.js, and Hideshow.js,and add the post-obit code,
- Comp1.js
- Comp2.js
- Hideshow.js
Now open up Comp1.js file and add together following code.
- import React, { Component } from "react" ;
- class Comp1 extends Component {
- constructor() {
- super();
- this .state = {
- proper name:"ReactJS"
- };
- }
- render() {
- return <div>This is component1</div>;
- }
- }
- consigndefault Comp1;
Now open Comp2.js file and add following code.
- import React, { Component } from "react" ;
- class Comp2 extends Component {
- constructor() {
- super();
- this .state = {
- name:"ReactJS"
- };
- }
- render() {
- return <div>This is component2</div>;
- }
- }
- consigndefault Comp2;
Now open Hideshow.js file and add together the post-obit code
- import React, { Component } from 'react'
- import Comp1 from"./Comp1" ;
- import Comp2 from"./Comp2" ;
- consignclass Hideshow extends Component {
- constructor() {
- super();
- this .country = {
- proper noun:"ReactJS" ,
- showHideComp1:fake ,
- showHideComp2:imitation ,
- };
- this .hideComponent = this .hideComponent.demark( this );
- }
- hideComponent(name) {
- console.log(proper noun);
- switch (name) {
- instance "showHideComp1" :
- this .setState({ showHideComp1: ! this .state.showHideComp1 });
- pause ;
- case "showHideComp2" :
- this .setState({ showHideComp2: ! this .state.showHideComp2 });
- break ;
- default :
- null;
- }
- }
- return() {
- const { showHideComp1, showHideComp2 } = this .state;
- return (
- <div>
- <divclass = "col-sm-12 btn btn-info" >
- Show Hide component on Click in React JS App
- </div>
- {showHideComp1 && <Comp1 />}
- <hour />
- {showHideComp2 && <Comp2 />}
- <hr />
- <div>
- <push className="btn btn-info" onClick={() => this .hideComponent( "showHideComp1" )}>
- Click to hide Demo1 component
- </button>
- <button className="btn btn-info" onClick={() => this .hideComponent( "showHideComp2" )}>
- Click to hide Demo2 component
- </button>
- </div>
- </div>
- );
- }
- }
- exportdefault Hideshow
Add reference of this component in app.js file,
- import React from 'react' ;
- import'./App.css' ;
- import Hideshow from'./Hideshow'
- office App() {
- return (
- <div className="App" >
- <Hideshow/>
- </div>
- );
- }
- exportdefault App;
Now run the project by using 'npm start' and check the result.
Summary
In this article, nosotros learned how to testify and hide content of a kid component in a parent component.
Source: https://www.c-sharpcorner.com/article/how-to-show-and-hide-component-in-react-application/
0 Response to "Button Onclick Show Click Again Remove Component React"
Enregistrer un commentaire