Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I change props of component that pass from higher component . reactjs

this is my upper component.

const Add_singlepage = () => {
    const [feed,setFeed] = useState(null);//getFeddd.php
    const [show,setShow] = useState({sh_categorytitle:true,sh_category:false});

    return (
      <div>
          <Nav1 />
          <div className={style.modal}>
          {
             show ?
              <CategoryTitle show={show} getFeed={feed} /> //consider this - I pass show data
              :
              <Category />
          }
          </div>
      </div>
   );
 };

I pass some data as props in categoryTitle and I want control and change it in them.
this is my CategoryTitle

import React from 'react';

const CategoryTitle = (props) => {
const {show} = props;
const next1 = ()=>{
    setShow({...show,sh_category:true,sh_categorytitle:false});
}
return (
       <div>
          ali
          <button onClick={next1} >reza</button>
       </div>
    );
};

export default CategoryTitle;

I know there is a wy like context – but I dont want do that.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

pass setShow too, as props, and this is what we call it callback works,

<CategoryTitle show={show} setShow={setShow} getFeed={feed} /> 

this is how call it in child,

const CategoryTitle = (props) => {
    const {show, setShow} = props;
    const next1 = () => {
        setShow({...show,sh_category:true,sh_categorytitle:false});
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading