1

We currently have our website built with Wordpress and I have built a component in React that we want to conditionally render on the page.

I have the component built and bundled with Webpack for production but cannot figure out how to add it to our website and render the component on the page

edit I will also need to conditionally render the component (form submit, button click, etc.)

Has anyone successfully done this?

2 Answers 2

3

Since you've built and bundled your component already, you only need to do three things:

  1. Add the bundled JavaScript (the Webpack output) as a script tag in whatever page you want the component to be on (preferably at the bottom of the <body>).
    Example: <script src="wp-content/react/bundle.js"></script>

  2. Add the root element that React will bind to to the same page.
    Example: <div id="react-root"></div>

  3. In your component file, render the component to the root element.
    Example: ReactDOM.render(<Component />, document.getElementById('react-root'));


To dynamically show and hide the React component based on something that happens outside the component file, you need to create a way to connect the outside (the DOM) to the inside (React). The easiest way to do this is with a global variable attached to window.

In your React component, add the componentWillMount method that defines a global variable:

componentWillMount() {
    window.showComponent = (option) => {
        // "option" should be true or false
        this.setState({ display: option });
    }
}

Based on the value passed to setState above, you'll need to add the display property to your component's state:

constructor(props) {
    super(props);
    this.state = {
        display: false
    };
}

Now to make the component show or hide based on the value of this.state.display inside the render() method:

render() {
  if (this.state.display) {
    return (
      ...
    )
  } else {
    return null;
  }
}

All that's left to do is use showComponent(true) or showComponent(false) in your code that handles the form.

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

3 Comments

This is great, I managed to get it rendered on my webpage! My only problem now is that I want to only conditionally render it. For example when a user submits a form, it would create the div with id="react-root". However, I get errors when the script runs but no element with that id exists yet
It's not possible to dynamically add/remove the React root element since the app binds to it on page load. To do this you'll need to take a few extra steps. I'll edit it into my answer.
Ok, I got it to work. What I did was to actually create the root element dynamically on the non-react page and then add the script to the page afterwards
0

In your React project add the file to the global scope (window) like this:

window.myReactComponents = {
  myFirstComponent: () => <MyFirstComponent/>
}

Once this is bundled reference the bundlejs file on your non-react page.

In your non react page in the global scope reference ReactDOM.render with the root element which it should use to render like this:

window.useComponent = {
  renderMyFirstComponent : ReactDOM.render(
    window.myReactComponents.myFirstComponent(),
    document.getElementById('myReactElement')
  )
};

That's it!

Live example

ReactDOM.render Documentation

1 Comment

Hey thanks, this definitely does work, however for larger bundled components the other answer works a little better. Appreciate the answer though!

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.