skip to Main Content

Here’s my example

// app.js
import {PostTitle} from './components/PostTitle.jsx';
function App() {
 render (
    <PostTitle data={data} />
)
}

// PostTitle.js
export function PostTitle({data}) {
    const [empty, updateEmpty] = useState(false);
    render (
       { empty && <div>component empty</div> }
    )
}

How to change empty to true from App

I try ref but it’s confusing, may be is simplest way, because I have more than one child components

2

Answers


  1. One thing you can do is to move this line const [empty, updateEmpty] = useState(false); in the parent component to check if the data is empty and set the state and pass that information as props to the child.

    Login or Signup to reply.
  2. The first thing you need to do is define the state in the App file. Then, you can pass this state to your child component as a prop.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search