2

I'm making changes to an existing code base.
Below is a snippet of the code with details hidden:

class Config extends Component {
  .
  .
  .
  render() {
    const { onSubmit, isConfigValid } = this.props;

    return (
      <Form>
        .
        .
        .
      </Form>
    );
  }
}

I need to change the <Form> component to a <Dialog> component and the implementation notes for this component give an example usage as:

render(
  <Dialog>
    .
    .
    .  
  </Dialog>, document.getElementById('root')
); 

How do I incorporate this into the current format where there is a return within the render?
I have tried simply replacing the <Form> tags with <Dialog> tags but I don't know where to put the document.getElementById('root') and the Dialog box appears in the wrong position without this.

Any help appreciated!

6
  • 1
    Just return a Dialog component. The example is using the dialog as the root component to ReactDOM.render(…) Commented Aug 6, 2018 at 14:40
  • 1
    2nd render seems to be ReactDOM.render since that's where you specify the container to show the component. Commented Aug 6, 2018 at 14:42
  • You could just create a new Dialog component and return it whenever you need. Commented Aug 6, 2018 at 14:43
  • @Li357 but what about document.getElementById('root'), where does that go Commented Aug 6, 2018 at 14:44
  • 1
    @tnoel999888 The example is using ReactDOM.render. If you have a standalone component then you don't need it (provided that you actually render the component in the first place) Commented Aug 6, 2018 at 14:46

1 Answer 1

3

As you can see you use ReactDOM.render(...) where you want to render your top component into the DOM. Here, it is Config.

class Config extends React.Component {
  render() {
    return (
      //<Form>, Instead use <Dialog> here
      <Dialog>
        <h1>Hello</h1>
        <p>Foo</p>
      </Dialog>
      //</Form>
    );
  }
}

// Instead of using in the same file, probably you will import it:
// import Dialog from "./where_is_this_Dialog";
const Dialog = (props) => (
  <div>{props.children}</div>
);

ReactDOM.render(
  <Config />,
  document.getElementById("root")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

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

2 Comments

Thanks for the answer! However I'm getting a message "ESLint: 'children' is missing in props validation (react/prop-types)". I don't really understand the const Dialog = (props) => (... bit, I've already imported Dialog, do I still need this bit of code in that case?
For prop types, it is a warning for the children I passed to Dialog component. I did this because you exampled this component like it waits some children. If you show us Dialog component we can make better suggestions. For the last question, of course you don't need the const Dialog ... part, I just mimicked your component here. You will import it then use it.

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.