简体   繁体   English

React build删除组件类的名称

[英]React build removes name of component class

I'm making a fullscreen rotating noticeboard in React with create-react-app. 我正在使用create-react-app在React中制作全屏旋转公告板。

The App component contains many Screen components, each one containing one child component (named such as WiFiPassword, OpenTimes, Events...) - and the Screen component checks if the current item from App is the name of it's child, and if it is then it shows that screen, if not then it hides it. App组件包含许多Screen组成,每一个包含一个子组件(命名如WiFiPassword,OpenTimes,活动...) -和Screen组件检查是否从当前项目App是它的名字的孩子,如果是然后显示该屏幕,如果没有,则将其隐藏。

This is the code where the values are checked: 这是检查值的代码:

class Screen extends Component {

  componentDidUpdate() {
    const active = this.props.active; // The active item, from parent state
    const elemName = this.props.children.type.name; //The name of the child class
    ...
    if(active === elemName){
      //If we have a match, then show this screen
    }
  }
  ...
}

This works really well when running in development. 在开发中运行时,这确实很好。

When I run npm run build and the process is finished, I load up the app and it doesn't work - it appears that the build process scrubs the class names from elements, which then means nothing is ever shown. 当我运行npm run build并完成该过程时,我加载了该应用程序,但它不起作用-似乎该构建过程从元素中清除了类名,这意味着什么都没有显示。

When I console.log() both the active prop and the child class name I get this: 当我console.log() ,活动道具和子类名称都得到:

active: WiFiPassword

elemName: t

Whatever screen it is on, elemName , which is the name of Screen 's child, is always t 无论屏幕在哪个屏幕上, elemName (它是Screen的孩子的名字)始终为t

Is there a way to retain the name of a component in react through the build process? 有没有办法在构建过程中保留组件名称以进行响应?

It looks like, you should switch to use react-router-dom, because you will have a good way to manage your screens. 看起来,您应该切换为使用react-router-dom,因为您将有一种管理屏幕的好方法。 There is a good component Switch, that shows one component at the time: 有一个很好的组件Switch,它同时显示一个组件:

import { Switch, Route } from 'react-router'

<Switch>
  <Route exact path="/" component={Home}/>
  <Route path="/about" component={About}/>
  <Route path="/:user" component={User}/>
  <Route component={NoMatch}/>
</Switch>

more information here: https://reacttraining.com/react-router/web/api/Switch 此处提供更多信息: https : //reacttraining.com/react-router/web/api/Switch

So I found a way around this: instead of relying on the class name, I instead imported every one of my <Screen> elements using a screens/index.js file: 因此,我找到了一种解决方法:我不再依赖类名,而是使用screens/index.js文件导入了每个<Screen>元素:

... export {default as Welcome} from './Welcome' export {default as WiFi} from './WiFi' ...

Then imported that index, file with each screen available to me: 然后导入该索引,并显示每个屏幕可用的文件:

import * as Screens from './screens'

Then I use whatever screen I want, depending on the same logic from within App: 然后,根据应用程序中的相同逻辑,使用所需的任何屏幕:

const Component = Screens[this.state.slide]; <Component/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM