H
H
Hlib2019-08-23 13:14:54
React
Hlib, 2019-08-23 13:14:54

How to switch styles for different components?

Hello, a long time ago I came across such a problem as switching styles in different components.
What does it mean:
We have two components:
1.NewsBigScreen
2.NewsElement
The first one is responsible for rendering the popup with darkened background
The second one simply renders the 'news'.
The problem is that two different buttons on different elements are responsible for switching styles.
This is what it looks like:

const NewsElement = (props) =>{

  const [display, setDisplay] = useState('none');

  const openBigScreen = () =>{
      document.body.style.overflow = 'hidden';
      setDisplay('flex')
    }

  return(
    <React.Fragment>
      <NewsBigScreen display={display}/>
      <div className='newsElement' onClick={openBigScreen}>
        <h2>{props.name}</h2>
        <img src={logo} />
        <p> {props.description}</p>
      </div>
    </React.Fragment>
  )
}

const NewsBigScreen = (props) =>{
  const [display, setDisplay] = useState('none')

  useEffect(()=>{
    setDisplay(props.display)
  })

  const closeBigScreen = () =>{
      setDisplay('none')
  }
  return(
    <div className='newsBigScreenWrap' style={{display: display}}>
      <div className='blackBackground'>
      </div>
      <div className='NewsBigScreen' >
        <input type='submit' onClick={closeBigScreen}/>
      </div>
    </div>
  )
}

How to pass props from one element to another and back?
Redax will not help, because. it will change the style for all elements, but I only need one
(newsBigScreen)
5d5fbb9c3d29d389810570.jpeg
(newsElement) (opens by clicking on one of the blocks)
5d5fbc010ac6e113422160.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-08-23
@Mysianio

The approach with in React development is not accepted. Conditional renderingdisplay: none

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question