theme development - How to attach different React Components to different Dom Roots using the new WP wordpressscripts webpackbab

Since WP now comes with webpackbabel build baked into core it is so easy to integrated React in WP. I am working on a t

Since WP now comes with webpack/babel build baked into core it is so easy to integrated React in WP. I am working on a theme and created my React Blog Page to list all of my posts. Works perfectly! Since I am using the "@wordpress/scripts": "3.2.1" I write my React files in multiple components but my main Root app is in a src/index.js file. which attaches to one dom root. When I try to had a second dom root to another template for the single posts, I keep getting a Target not a Dom Object. So my question is: Using the WP Build Scripts package, how can I create multiple React Components and attach them to multiple unique Dom Roots on my different page templates in my theme.
so my main index.js file when I write a second React Dom render to attach a total different React Component to a total different dom root, it does not work. How can I accomplish this task successfully. I do NOT want to make them Gutenberg Blocks. So trying to attach this second React Component to totally different dom root element will not work. get error Target container is not a DOM element.

for example my src/index.js file:

import PostList from './components/PostList';
import SinglePost from './components/SinglePost';


class App extends React.Component {
    render() {
      return (
        <div>
            <Header title="test title" />
            <PostList/>
        </div>
      );
    }
  }

ReactDOM.render(<App />, document.getElementById("app"));


class SinglePostApp extends React.Component {
    render() {
        return (
            <div>
               <SinglePost />
            </div>
        )
    }
}
ReactDOM.render(<SinglePostApp />, document.getElementById("spApp"));


Since WP now comes with webpack/babel build baked into core it is so easy to integrated React in WP. I am working on a theme and created my React Blog Page to list all of my posts. Works perfectly! Since I am using the "@wordpress/scripts": "3.2.1" I write my React files in multiple components but my main Root app is in a src/index.js file. which attaches to one dom root. When I try to had a second dom root to another template for the single posts, I keep getting a Target not a Dom Object. So my question is: Using the WP Build Scripts package, how can I create multiple React Components and attach them to multiple unique Dom Roots on my different page templates in my theme.
so my main index.js file when I write a second React Dom render to attach a total different React Component to a total different dom root, it does not work. How can I accomplish this task successfully. I do NOT want to make them Gutenberg Blocks. So trying to attach this second React Component to totally different dom root element will not work. get error Target container is not a DOM element.

for example my src/index.js file:

import PostList from './components/PostList';
import SinglePost from './components/SinglePost';


class App extends React.Component {
    render() {
      return (
        <div>
            <Header title="test title" />
            <PostList/>
        </div>
      );
    }
  }

ReactDOM.render(<App />, document.getElementById("app"));


class SinglePostApp extends React.Component {
    render() {
        return (
            <div>
               <SinglePost />
            </div>
        )
    }
}
ReactDOM.render(<SinglePostApp />, document.getElementById("spApp"));


Share Improve this question edited May 29, 2019 at 12:56 phptherightway asked May 29, 2019 at 12:48 phptherightwayphptherightway 696 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

ok, after hours of research and trying different things I found my answer.

You can extend the WordPress webpack config file. Here is documentation from WordPress

https://developer.wordpress/block-editor/packages/packages-scripts/#advanced-usage

So here is my new webpack config file located in my theme root directory.I have moved the SinglePost React code into its own file, singlePost.js. Now when I run npm run build I get two different files. I also had to add this new build js file in my functions.php enqueue function in order to load my new javascript file.

const defaultConfig = require("./node_modules/@wordpress/scripts/config/webpack.config");


module.exports = {
  ...defaultConfig,
    entry: {
    ...defaultConfig.entry,
        index: path.resolve( process.cwd(), 'src', 'index.js' ),
        singlePost: path.resolve( process.cwd(), 'src', 'singlePost.js' )
    }
};


发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745450137a4628229.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信