F
F
fedorova_04102020-05-13 20:12:05
React
fedorova_0410, 2020-05-13 20:12:05

How to show/hide a block on click in react?

You need to rewrite the component to a class. To hide/appear the fielset block when clicking on h2.

const SelectTaxon = (props) => {
  let selectTaxonCheckbox = props.selectTaxonData.map((input) => (
    <InputCheckbox
    id={input.id}
    htmlFor={input.htmlFor}
    title={input.title} />
  ))


  return (
    <div className={styles.taxon}>
      <h2 className={styles.h2}>Назва Таксону</h2>
      <fieldset className={styles.fieldset + ' ' + styles.fieldsetActive}>
        <legend className={styles.legend}>Охоронний статус</legend>
        {selectTaxonCheckbox}
        <legend className={styles.legend + ' ' + styles.legendTaxon}>
          Джерело даних
        </legend>
      </fieldset>
    </div>
  )
}

export default SelectTaxon

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tigran Abrahamyan, 2020-05-13
@fedorova_0410

import React, { useState } from 'react';

const SelectTaxon = (props) => {
  const [isShown, setIsShown] = useState(true);

  const toggleFIeldset = () => setIsShown(!isShown);

  let selectTaxonCheckbox = props.selectTaxonData.map((input) => (
    <InputCheckbox
      id={input.id}
      htmlFor={input.htmlFor}
      title={input.title} />
  ))


  return (
    <div className={styles.taxon}>
      <h2 className={styles.h2} onClick={toggleFIeldset}>Назва Таксону</h2>
      {isShown &&
        <fieldset className={styles.fieldset + ' ' + styles.fieldsetActive}>
          <legend className={styles.legend}>Охоронний статус</legend>
          {selectTaxonCheckbox}
          <legend className={styles.legend + ' ' + styles.legendTaxon}>
            Джерело даних
          </legend>
        </fieldset>
      }
    </div>
  )
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question