- Hands-On Design Patterns with React Native
- Mateusz Grzesiukiewicz
- 387字
- 2021-08-13 15:12:59
A beginner's guide to naming
Naming may sound trivial, but there are some standard practices in React that you should comply with. These practices may vary from project to project, but keep in mind that you should respect at least the ones that are mentioned here. In other cases, check your project's style guide and possibly your linter configuration.
A component name should start with an uppercase letter unless it's a HOC. Use the component name as the filename. The filename should be in UpperCamelCase (for more information on CamelCase, see https://en.wikipedia.org/wiki/Camel_case):
// bad
someSection.js
// good
SomeSection.js or SomeSection.jsx
// Current Airbnb style guide recommends .jsx extension though.
The following are rules on importing your component:
// bad import App from './App/App'; // bad import App from './App/index'; // good import App from './App';
If it's HOC, start its name with a lowercase letter in lower CamelCase, for instance, makeExpandable.
Airbnb also suggests that you take care of the name of the inner component. We need to specify a displayName prop to do, as in the following:
// Excerpt from
// https://github.com/airbnb/javascript/tree/master/react#naming
// bad export default function withFoo(WrappedComponent) { return function WithFoo(props) { return <WrappedComponent {...props} foo />; } } // good export default function withFoo(WrappedComponent) { function WithFoo(props) { return <WrappedComponent {...props} foo />; } const wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || 'Component'; WithFoo.displayName = `withFoo(${wrappedComponentName})`; return WithFoo; }
This is a valid point as in some tools you may benefit from seeing the proper component names. Following this pattern is optional and up to the team to decide upon.
When defining new props, please avoid the common props that used to mean something else. An example may be the style prop we used to pass styles to our components.
Please check out the following links to check what props you should avoid using:
- Props corresponding to your application layout:
- Props reserved for component styling, as it may create confusion:
Don't get too scared. It will feel more natural sooner or later.
- Python科學(xué)計(jì)算(第2版)
- LabVIEW Graphical Programming Cookbook
- Boost C++ Application Development Cookbook(Second Edition)
- 程序員面試筆試寶典
- AngularJS Web Application Development Blueprints
- Unity Virtual Reality Projects
- Learn Swift by Building Applications
- Cassandra Data Modeling and Analysis
- Learning Network Forensics
- 微信小程序開發(fā)解析
- 網(wǎng)站構(gòu)建技術(shù)
- Building RESTful Python Web Services
- Illustrator CC平面設(shè)計(jì)實(shí)戰(zhàn)從入門到精通(視頻自學(xué)全彩版)
- MINECRAFT編程:使用Python語(yǔ)言玩轉(zhuǎn)我的世界
- Learning Splunk Web Framework