0

I am new to React Hooks. I have been learning this language with a number of questions in stackoverflow. Even though I have explored the other questions here for 3 days to figure out my following issue, I have not found a proper way.

The following link is my simplified code:

import React, { useState } from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  const [render, setRender] = useState(true);
  const [show, setShow] = useState(true);
  return (
    <div className="App">
      <button onClick={() => setRender(!render)}>Render</button>
      {show ? (
        <div>
          <h1>Hello CodeSandbox</h1>
          <button onClick={() => setShow(!show)}>Hide</button>
        </div>
      ) : /// setShow(true) <- How can I update it?

      null}
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

From my example, what I want to try is, when Hide button is clicked, Hello CodeSandbox disappears because it is null after show becomes false. However, the issue is when I click Render button, it should display Hello CodeSandbox again but it doesn't. I believe the reason is because show state still remains false. Thus, I want to update show state to true once Render button is clicked so that Hello CodeSandbox appears.

Is it possible if I can update state before using null? Or is there any better way?

1
  • 1
    Please include a minimal reproducible example in the question itself. Avoid relying only on off-site resources for your question. Commented Apr 27, 2021 at 19:53

2 Answers 2

3

You are rendering based on the state show, so your Render button should manipulate that state, just set it back to true when it is clicked:

function App() {

 const [show, setShow] = useState(true);
  return (
    <div className="App">
      <button onClick={() => setShow(true)}>Render</button>
      {show ? (
        <div>
          <h1>Hello CodeSandbox</h1>
          <button onClick={() => setShow(false)}>Hide</button>
        </div>
      ) :
      null
     }
    </div>
  );
}

Edit React Hooks Counter Demo (forked)

Sign up to request clarification or add additional context in comments.

Comments

1

Just create a function that will update both states and call it using your render button:

function App() {
  const [render, setRender] = useState(true);
  const [show, setShow] = useState(true);

  const update = () => {
    setRender(!render);
    setShow(!show)
  }
  
  return (
    <div className="App">
      <button onClick={update}>Render</button>
      {show ? (
        <div>
          <h1>Hello CodeSandbox</h1>
          <button onClick={() => setShow(!show)}>Hide</button>
        </div>
      ) : /// setShow(true) <- How can I update it?

      null}
    </div>
  );
}

Edit React Hooks Counter Demo (forked)

1 Comment

You should paste your code here as well (in case the link dies in the future).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.