简体   繁体   English

如何使用与组件反应来隐藏我的 div?

[英]How can I do to hide my div using react with components?

I have this code for the App :我有这个应用程序的代码:

import React, {Component} from 'react';
import App1 from './App1';

class App extends Component {
    render(){
        return (
                    <>
                        <App1/>
                        <div>
                        <h1>Hello</h1>
                        </div>
                    </>
                );
        }
}

export default App;

And this code is for the App1此代码适用于 App1

import React, {Component} from 'react';

class App1 extends Component {
    render() {
        return (
                    <>
                    <button>Hide</button>
                    </>
                );
    }
}

export default App1;

I would like when I click on the Button to hide my div which displays "Hello".我希望当我单击按钮时隐藏显示“Hello”的 div。 But I have no idea to do this ?但我不知道这样做?

Could you help me please ?请问你能帮帮我吗 ?

Thank you very much !非常感谢 !

You can hide the div in the parent component ie (App.js) by using props.您可以使用 props 隐藏父组件中的 div,即(App.js)。 So here are the steps you need to follow:因此,您需要遵循以下步骤:

  1. create a function named as handleHide in App component, and pass it as a prop to App1 component.在 App 组件中创建一个名为 handleHide 的函数,并将其作为 prop 传递给 App1 组件。
  2. Define a state named as hide in App component and pass it as a prop in App1 component.在 App 组件中定义一个名为 hide 的状态,并将其作为 App1 组件中的 prop 传递。
  3. Inside App1 component use the hide prop to change the text of button(it's bonus).在 App1 组件内部使用 hide 道具更改按钮的文本(这是奖励)。
  4. Assign handleHide function passed as prop from App to App1 component's button element's onClick .将作为道具从 App 传递给 App1 组件的按钮元素的 onClick 的 handleHide 函数分配。

Here are the files: App.js以下是文件:App.js

import React, { Component } from "react";
import App1 from "./App1";

class App extends Component {
  state = {
    hide: false
  };
  handleHide = () => {
    this.setState({ hide: !this.state.hide });
  };
  render() {
    return (
      <>
        <App1 handleHide={this.handleHide} hide={this.state.hide} />
        <div>{!this.state.hide && <h1>Hello</h1>}</div>
      </>
   );
  }
}
export default App;

And App1.js will be: App1.js 将是:

import React, { Component } from "react";

class App1 extends Component {
  render() {
    return (
      <>
        <button onClick={this.props.handleHide}>
          {this.props.hide ? "Show" : "Hide"}
        </button>
      </>
    );
  }
}

export default App1;

You can see the full working code here .您可以在此处查看完整的工作代码。

You create a state in App.js and pass those state down to App1.js , which look like this您在App.js创建一个状态并将这些状态传递给App1.js ,如下所示

 class App extends Component { constructor(){ super(); this.state = { hidden: false }; this.changeHiddenStatus = this.changeHiddenStatus.bind(this) } changeHiddenStatus = () => { this.setState(state => ({ hidden: !state.hidden })) } render(){ return ( <> <App1 handleClick={this.changeHiddenStatus}/> <div> <h1>Hello</h1> </div> </> ); } }

And then in the App1.js you did this然后在App1.js你做了这个

 class App1 extends Component { render() { return ( <> <button onClick={props.handleClick}>Hide</button> </> ); } }

These are some basic React stuff, so if you don't get it I suggest you should read the React doc again.这些是一些基本的 React 内容,所以如果你不明白,我建议你应该再次阅读 React 文档。

Using class component is perfectly fine.使用类组件非常好。 You can use functional component to use react hooks it makes your code more readable and less code.您可以使用功能组件来使用 React 钩子,它使您的代码更具可读性和更少的代码。

App.js应用程序.js

import React, { useState } from "react";
import App1 from "./App1";
export default function App() {
  const [show, setShow] = useState(true);
  return (
    <>
      <App1 setShow={setShow} show={show} />
       <div>{show && <h1>Hello</h1>}</div>
    </>
  );
}

App1.js应用1.js

import React from "react";

export default function App1({ setShow, show }) {
  return (
    <>
      <button onClick={() => setShow(!show)}>{show ? "Hide" : "Show"}</button>
    </>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM