Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

How to hide component in React

import React, { useState } from 'react';

const Component = () => {
  const [show, setShow] = useState(false);
  return(
    <>
      <button onClick={() => setShow(prev => !prev)}>Click</button>
      {show && <Box>This is your component</Box>}
    </>
  );
}

export default Component
Comment

react hide element

React circa 2020
In the onClick callback, call the state hook's setter function to update the state and re-render:

const Search = () => {
  const [showResults, setShowResults] = React.useState(false)
  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>
)

ReactDOM.render(<Search />, document.querySelector("#container"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>

<div id="container">
  <!-- This element's contents will be replaced with your component. -->
</div>
Comment

Show and Hide element in react

return(
  <nav className="nav__bar">
    <ul className="menu">
      <li className="menu__icon" onClick={() => setShow(currentShow => !currentShow)}>
        <box-icon name='menu' color="floralwhite" size="md"/>
        { show ? <Curtain/> : null }
      </li>
    </ul>
  </nav>
);
Comment

hide show object in react

const Search = () => {
  const [showResults, setShowResults] = React.useState(false)
  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>
)

ReactDOM.render(<Search />, document.querySelector("#container"))
Comment

PREVIOUS NEXT
Code Example
Javascript :: nodejs exit code 
Javascript :: nodejs return code 
Javascript :: route parameter in node 
Javascript :: jquery select input 
Javascript :: make multiple api call using promise.all 
Javascript :: jquery recharger la page 
Javascript :: append http to url 
Javascript :: get duplicate value javascript 
Javascript :: nextjs query parameter 
Javascript :: geojson 
Javascript :: js event on change focus 
Javascript :: ex:loop array 
Javascript :: send post request 
Javascript :: call multiple functions onclick react 
Javascript :: get json data into object 
Javascript :: create secure jwt secret key using node crypto 
Javascript :: javascript array slice 
Javascript :: fontawesome icon size 1.5 angular 
Javascript :: node assert 
Javascript :: application pool angular 8 
Javascript :: how to store object in session storage 
Javascript :: javascript polling 
Javascript :: javascript statement 
Javascript :: code for javascript message box 
Javascript :: fetch post js 
Javascript :: tableau js 
Javascript :: aes 256 file encryption node js 
Javascript :: how to detect if an video is over js html 
Javascript :: onclick on fragment react 
Javascript :: js changing selected option by index 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =