Home > Web Front-end > JS Tutorial > How to Dynamically Show or Hide Elements in React Using State?

How to Dynamically Show or Hide Elements in React Using State?

Mary-Kate Olsen
Release: 2024-11-06 09:58:02
Original
934 people have browsed it

How to Dynamically Show or Hide Elements in React Using State?

How to Show or Hide an Element in React

The Problem

Beginners often face difficulties in displaying or concealing elements in React using click events. This guide provides a native React solution, eliminating the need for external libraries.

Example Code

Consider the following React code:

<code class="js">var Search = React.createClass({
    handleClick: function (event) {
        console.log(this.prop);
    },
    render: function () {
        return (
            <div className="date-range">
                <input type="submit" value="Search" onClick={this.handleClick} />
            </div>
        );
    },
});

var Results = React.createClass({
    render: function () {
        return (
            <div id="results" className="search-results">
                Some Results
            </div>
        );
    },
});

React.renderComponent(<Search />, document.body);</code>
Copy after login

Solution

Using the latest React features, the solution involves:

React State Management

Leverage React's state hooks to handle the toggle between showing and hiding the element. State management determines whether the element should be rendered or not.

Component Code

<code class="js">const Search = () => {
  // Initialize state to hide results
  const [showResults, setShowResults] = React.useState(false);

  // Toggle results visibility on click
  const onClick = () => setShowResults(true);

  return (
    <div>
      <input type="submit" value="Search" onClick={onClick} />
      { showResults ? <Results /> : null }
    </div>
  );
};

const Results = () => (
  <div id="results" className="search-results">
    Some Results
  </div>
);

// Render the component
ReactDOM.render(<Search />, document.getElementById('container'));</code>
Copy after login

Key Takeaways

  • State management allows dynamic rendering based on user interaction.
  • By updating the state, React efficiently re-renders the affected components.
  • This solution is a versatile approach that can be applied to various scenarios requiring element visibility control in React.

The above is the detailed content of How to Dynamically Show or Hide Elements in React Using State?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template