当前位置:主页 > 资料 >

Why this React code not work in production
栏目分类:资料   发布日期:2017-10-31   浏览次数:

导读:本文为去找网小编(www.7zhao.net)为您推荐的Why this React code not work in production,希望对您有所帮助,谢谢! Today I was migrating a personal project to create-react-app , then I found my RadioButton stops working

本文为去找网小编(www.7zhao.net)为您推荐的Why this React code not work in production,希望对您有所帮助,谢谢! copyright www.7zhao.net



Today I was migrating a personal project to create-react-app , then I found my RadioButton stops working in production while it works perfectly in development. After I figured out why, I think it worth noticing people why. 本文来自去找www.7zhao.net

What’s the code

It’s about auto-binding, where the <RadioButtons> will bind some props to its children if the child pass the following check.

copyright www.7zhao.net

isRegisteredComponent = child=> (
    typeof child.type === 'function' &&
    child.type.name.toString() === 'RadioButton'
);
 copyright www.7zhao.net 

It works flawlessly. But after yarn build , which create-react-app will generate production code, it stops working.

www.7zhao.net

Why

The reason is simple, after uglify , the name of the component get changed from RadioButton to r , then this check failed to pass, so it stops working. 欢迎访问www.7zhao.net

How to solve

This is a simple case, consider that the <RadioButton> will receive a unique prop named option before the auto binding happens, I just use that as a flag to do the change. Then everything is fine. copyright www.7zhao.net

isRegisteredComponent = child=> (
    child.props.hasOwnProperty('option')
);
 去找(www.7zhao.net欢迎您 

A takeaway is: 本文来自去找www.7zhao.net

remember the fact that your code maybe change when build for production, and handle that case as well.

本文来自去找www.7zhao.net

Hope it helps. :)

去找(www.7zhao.net欢迎您

本文来自去找www.7zhao.net


本文原文地址:http://www.albertgao.xyz/2017/10/31/why-this-react-code-not-work-in-production/

以上为Why this React code not work in production文章的全部内容,若您也有好的文章,欢迎与我们分享! 去找(www.7zhao.net欢迎您

Copyright ©2008-2017去找网版权所有   皖ICP备12002049号-2 皖公网安备 34088102000435号   关于我们|联系我们| 免责声明|友情链接|网站地图|手机版