最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create React App for multiple apps? - Stack Overflow

programmeradmin3浏览0评论

Say I have alot of small apps, do I need to do a create-react-app for each one? Or can I have one create-react-app. The latter is my preference because I don't want a separate node_modules for each app. Because after the npm install it's hundred of megabytes.

If it's possible, what's the boiler I'd need to use to create multiple apps, is this is a webpack thing?

Say I have alot of small apps, do I need to do a create-react-app for each one? Or can I have one create-react-app. The latter is my preference because I don't want a separate node_modules for each app. Because after the npm install it's hundred of megabytes.

If it's possible, what's the boiler I'd need to use to create multiple apps, is this is a webpack thing?

Share Improve this question edited Feb 8, 2018 at 20:46 Shai UI asked Feb 8, 2018 at 20:36 Shai UIShai UI 52k77 gold badges217 silver badges316 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

You can use environment variables to do this.
Environment variables, that start with 'REACT_APP_' are available in the browser via process.env.REACT_APP_[NAME]

If you use an IDE like WebStorm, you can create different run configurations for every app and set Environment to REACT_APP_SETUP=app1 for example.

In the shell you can set it like this:

export REACT_APP_SETUP=app1
npm start

Then you just render different root ponents in your index.js

    // in index.js
    const setup = process.env.REACT_APP_SETUP;
    
    if (setup == "app1") {
        ReactDOM.render(<App1/>,document.getElementById('root'));
    } else {
        ReactDOM.render(<App2/>,document.getElementById('root'));
    }

To create new app, Yes we need to create every time, But if we want all small apps inside one big app we may develop those small apps as 'Components' (which performs individually) and integrate inside one application.

When you create an app via create-react-app you create a new app from ground up. these apps use separate configuration files and are not meant to "talk" to each other. what you could do is to put each "app.js" file in one shared react-app and then rearrange these classes to share their contents (see import/export). But from my understanding it is not possible to have multiple react apps in one giant app

You can consider using react-app-rewired

But you won't get any support from create-react-app team, since its your own config. The other obvious solution is to eject.

Reference https://medium./@timarney/but-i-dont-wanna-eject-3e3da5826e39

发布评论

评论列表(0)

  1. 暂无评论