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 :: how to update state in react 
Javascript :: sequelize association alias 
Javascript :: javascript prevent value change in select option 
Javascript :: React social login button 
Javascript :: iterate over json data javascript 
Javascript :: pure component 
Javascript :: remove object if key is duplicate javascript 
Javascript :: bind in javascript 
Javascript :: js download 
Javascript :: Searchkick::ImportError: {"type"="cluster_block_exception" 
Javascript :: how to check provided value is in array in javascript 
Javascript :: javascript scrollby div 
Javascript :: round innerhtml value down javascript 
Javascript :: ways of defining object js 
Javascript :: how to add image url in tailwindconfig .js 
Javascript :: claim faucets 
Javascript :: how to make if method inside an if methen in fandom 
Javascript :: expiry data of jwt token 
Javascript :: while loops js 
Javascript :: multiselect_3 
Javascript :: angular hash sign in url 
Javascript :: javascript create date object for midnight for a timezone 
Javascript :: setTilme out js 
Javascript :: how to send message to user in socket.io 
Javascript :: write data in props.histroy.push in react component 
Javascript :: alpinjs 
Javascript :: use params in Class based component 
Javascript :: timer stop button 
Javascript :: popos not showing applications 
Javascript :: check if specific letter exist in string javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =