0

I have a parent componet like this, just to show the dialog

The Child Component ( Main to show dialog)

   export const MedicalRecord = memo(function MedicalRecord() {
  // const onPressViewAll = useCallback(() => {}, [])
  const [show, setShow] = useState(false) ///to show dialog

  function hanndleDialog() {
    setShow(!show) set to show dialog
  }

  // useEffect(() => {
  //   if (show == true) {
  //     setShow(!show)
  //   }
  // },[show])
  return (
    <SummaryViewContainer
      count={5}
      title={"dashboardScreen.medicalRecords.title"}
      onPress={() => {
        hanndleDialog()
      }}
    >
      <View>
        {show && (
          <ProgressDialog
            show={show} //pass t
            callback={() => {
              hanndleDialog()
            }}
          />
        )}
        <RecordItem />
        <RecordItem />
        <RecordItem />
      </View>
    </SummaryViewContainer>
  )
})

And parent componet to show this dialog

  export default function DialogTesting(show: boolean, { callback }) {
  const [showDialog, doShow] = useState(show) //show to present show in child

  return (
    <View>
      {/* <Button
        title="click"
        onPress={() => {
          setShow(true)
        }}
      >
        <Text>Show dialog</Text>
      </Button> */}
      <Dialog
        visible={showDialog}
        title="Record New Progress"
        style={DIALOG}
        onClose={() => {
          doShow(false)
          callback()
        }}
      >

But i cant figure out how to open dialog again when close the dialog, it only open for once, i try React Hook : Send data from child to parent component but not work !

How can i show dialog and when i click close button, the children return orignal state so i can click it again, thank you guy so much

Here is a short video of this problem https://recordit.co/0yOaiwCJvL

Lê Quốc Khánh
  • 545
  • 1
  • 6
  • 20
  • Can you share a minimum working snippet? – Shivam Sharma Aug 06 '21 at 14:06
  • Unluckily this code this is whole project and it not mine :((( , so it very difficult to share working code – Lê Quốc Khánh Aug 06 '21 at 14:12
  • Then try removing `memo` as the props and states are the same so it can render the memorized component. I am just guessing. – Shivam Sharma Aug 06 '21 at 14:20
  • It not work :( , i try to do this all this day – Lê Quốc Khánh Aug 06 '21 at 14:22
  • Or you might need to [remove the use of the derived state](https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html) if you have i.e. copy prop to state, instead get a method as prop from parent to change the state of parent to show/hide and pass that as a prop to the dialog component. and handle show/hide of dialog based on that prop. – Shivam Sharma Aug 06 '21 at 14:23
  • Or you can choose the option to render the dialog component only if the parent state has `show=true` and pass a method from parent to dialog component to toggle that state from the dialog component itself. – Shivam Sharma Aug 06 '21 at 14:25
  • I will try it and feedback to you right now – Lê Quốc Khánh Aug 06 '21 at 14:35
  • 1
    [This is the sandbox](https://codesandbox.io/s/dreamy-tdd-ee8tk) for the last solution suggested. I hope this helps if I have understood the problem statement right. – Shivam Sharma Aug 06 '21 at 14:35
  • Is this the solution you were looking for? – Shivam Sharma Aug 06 '21 at 14:50

1 Answers1

1

I am assuming that you want to find a way to show hide a component based on click. So this is the sandbox for the same.

In this solution, instead of using a derived state, the state is held in the parent's state and the child is mounted/unmounted based on that state.

The state can be updated by a method present in the parent and this method is passed to the child to be triggered on the "hide child" button. The same method is used to show the child component as well.

Below is the core code for the same,

import React from "react";

const Dialog = ({ hideMe }) => {
  return (
    <div>
      <div>I am dialog</div>
      <button onClick={hideMe}>Hide me</button>
    </div>
  );
};
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { showDialog: false };
  }

  toggleDialog = () => {
    this.setState((prevState) => {
      return { showDialog: !prevState.showDialog };
    });
  };

  render() {
    return (
      <div>
        <div>I am parent.</div>
        <button onClick={this.toggleDialog}>Toggle Dialog</button>
        {this.state.showDialog ? <Dialog hideMe={this.toggleDialog} /> : null}
      </div>
    );
  }
}

export default App;
Shivam Sharma
  • 1,277
  • 15
  • 31
  • hey sir, but in my problem, there are a prop called `visiable` in parent componet, how can i access this in both parent and it child? – Lê Quốc Khánh Aug 06 '21 at 16:11
  • i mean, it have `visiable` to make dialog appear, then it have a bool call `show` to set value to `visiable` to false, there will be 2 props, right? So i wonder how can we make when `visiable` in dialog show true, in main screen, bool show show dialog return `false`? – Lê Quốc Khánh Aug 06 '21 at 16:20
  • if you mean that you have a hierarchy like this `parent1->parent2->child` and the state `visible` is in `parent1`, then you can pass the `parent1` state `visible` to `parent2` as prop and pass this prop unchanged to `child` eg. in `parent2` it will be `this.props.visible` instead of `this.state.showDialog` in above code. same the function will be in `parent1` and passed to `parent2` and then to `child` unchanged. – Shivam Sharma Aug 06 '21 at 17:36
  • 1
    Your welcome, if this is the solution you were looking for then please accept the answer. – Shivam Sharma Aug 07 '21 at 05:07
  • actually i it solve part of my problem , it would be great if you help me with that, that said, i got a chilren prop called `show` to show parent dialog , i use `react hook` to store state, it was `const[show,doShow]` =useState(false)` like i metion in my post, and i use that hook by passing prop to parent dialog , like `dialogFunc(show)` , but my problem is, i set the button in child( main screen) change hook to `true` when click( for showing the dialog) then, when i click , it open up dialog, but when i click dialog it still true, so i need to press 2 time to put the dialog again – Lê Quốc Khánh Aug 07 '21 at 05:26
  • How can i set `show` hook in children( then main file contain button to show dialog) to false every time i close dialog, like, reset state when close dialog , so i dont have to press it twice – Lê Quốc Khánh Aug 07 '21 at 05:27
  • Can you please show the component hierarchy and where the state to show hide and the function to toggle that exist, like I mentioned in my comment to this answer, sorry I am not able to understand. – Shivam Sharma Aug 07 '21 at 05:53
  • Hey i just update in my post , sir, sorry for bad explanation , please help – Lê Quốc Khánh Aug 07 '21 at 10:40