Display element based on event fired and props passed in

Albin M

I am trying, to manipulate another element, by, passing props directly to it, and then have it display itself. If I pass true/false.

Live running code:

https://codesandbox.io/s/keen-dan-rt0kj

  • I don't know if it's possible to have a system of objects, and based on an event, tell a parent to display a child.

App.js

import React from "react";
import "./styles.css";
import Content from "./components/Content";

export default class App extends React.Component {
  state = {
    display: false
  };

  render() {
    return (
      <div className="App">
        <button onClick={() => this.setState({ display: !this.state.display })}>
          Display div
        </button>
        <Content display={this.state.display} />
      </div>
    );
  }
}

./components/Content.js:

import React from "react";

export default class Content extends React.Component {
  constructor(props) {
    super();
    this.state = {
      display: props.display
    };
  }

  render() {
    const { display } = this.state;
    return (
      <div
        id="mydiv"
        className="mydiv"
        style={{ display: display ? "block" : "none" }}
      >
        <h3>A simple div</h3>
      </div>
    );
  }
}

Goal: I want to based on a state, and based on fired event, display an element that already in store of root.

EDIT: I am aware that, this exists and can be used: import PropTypes from 'prop-types', however, I am not sure this is good practice, since it requires some parent or some other component to implement the props.


JUST Tried:

App:

<Content display={this.state.display} content={"Hello World"} />

Content:

<h3>{this.state.content}</h3>

It seems the passed in text, stored in Content state = {content: props.content} does get displayed, wheres, the boolean value does not work directly. Is there something wrong with sending in a bool ?

enter image description here

Vibhav

try this in your Content Component

    export default class Content extends React.Component {
      constructor(props) {
        super();
        this.state = {
          
        };
      }
    
      render() {
        return (
         <>
          {this.props.display?(
            <div
            id="mydiv"
            className="mydiv"
            >
            <h3>A simple div</h3>
          </div>
          ):null}
         </>
        );
      }
    }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Why handleClick event is not fired from Child Element?

分類Dev

How to add additional props to a React element passed in as a prop?

分類Dev

ObjectListItem "press" event is not fired

分類Dev

Polymer paper-dropdown's core-select event fired by another element's core-selector

分類Dev

Extract data from fired event

分類Dev

event fired on control added to the listbox

分類Dev

Checking which event was fired in Event Subscriber

分類Dev

MessageSending event not being fired when sending an email

分類Dev

Why touchmove event is not fired after DOM changes?

分類Dev

Why could window scroll event not being fired

分類Dev

When is html languagechange event fired in the browser?

分類Dev

Is there a js event that being fired when selection starts?

分類Dev

Select event not fired on Google chart API for Sankey

分類Dev

Adding additional props to a component passed as a prop

分類Dev

React prevent remounting components passed from props

分類Dev

Props passed to child component are empty in constructor and componentDidMount()

分類Dev

Update variable based on time passed

分類Dev

React and jQuery event handler's fired in wrong order

分類Dev

Add a new Row in DataGridView with incremented value every time the event is fired

分類Dev

How to push new items into lisview after socketio event is fired in nativescript

分類Dev

Application_Start event fired on every postback - lost session and cache

分類Dev

How to find out which event listener has fired

分類Dev

Handle event fired when selected drop down option is selected again

分類Dev

Click event is fired more than once in Internet explorer 9

分類Dev

Display error message when autocomplete is fired if State is not chosen from dropdown

分類Dev

SVG image not getting passed to a function in an onclick event

分類Dev

$event passed with ng-click is undefined

分類Dev

How to properly append to string array that was passed as props in functional component

分類Dev

Bind event to wrapper element

Related 関連記事

  1. 1

    Why handleClick event is not fired from Child Element?

  2. 2

    How to add additional props to a React element passed in as a prop?

  3. 3

    ObjectListItem "press" event is not fired

  4. 4

    Polymer paper-dropdown's core-select event fired by another element's core-selector

  5. 5

    Extract data from fired event

  6. 6

    event fired on control added to the listbox

  7. 7

    Checking which event was fired in Event Subscriber

  8. 8

    MessageSending event not being fired when sending an email

  9. 9

    Why touchmove event is not fired after DOM changes?

  10. 10

    Why could window scroll event not being fired

  11. 11

    When is html languagechange event fired in the browser?

  12. 12

    Is there a js event that being fired when selection starts?

  13. 13

    Select event not fired on Google chart API for Sankey

  14. 14

    Adding additional props to a component passed as a prop

  15. 15

    React prevent remounting components passed from props

  16. 16

    Props passed to child component are empty in constructor and componentDidMount()

  17. 17

    Update variable based on time passed

  18. 18

    React and jQuery event handler's fired in wrong order

  19. 19

    Add a new Row in DataGridView with incremented value every time the event is fired

  20. 20

    How to push new items into lisview after socketio event is fired in nativescript

  21. 21

    Application_Start event fired on every postback - lost session and cache

  22. 22

    How to find out which event listener has fired

  23. 23

    Handle event fired when selected drop down option is selected again

  24. 24

    Click event is fired more than once in Internet explorer 9

  25. 25

    Display error message when autocomplete is fired if State is not chosen from dropdown

  26. 26

    SVG image not getting passed to a function in an onclick event

  27. 27

    $event passed with ng-click is undefined

  28. 28

    How to properly append to string array that was passed as props in functional component

  29. 29

    Bind event to wrapper element

ホットタグ

アーカイブ