Is there an official style guide or naming convention for React based projects?

前端 未结 5 1497
情歌与酒
情歌与酒 2021-01-04 04:42

I\'m setting up a React project with my team that will use mobX as state manager, plus TypeScript.

I\'ve seen a common pattern in the casing and naming patterns in R

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 05:01

    It is common across many languages to PascalCase their classes and have camelCase functions and variable names. A JS example,

    function hello() {console.log('world')};
    
    class Foo {
      say() { console.log('bar') }
    }
    let foo = new Foo();
    foo.say();
    

    components are often classes class Nav extends React.PureComponent and so the logical connection is to name the file containing the class similarly, resulting in matching case import statements import Nav from './Nav

    You may also have a utility file, which exports a function, not a class. Again, it's nice to have matching cases import hello from './hello'

    As such, you may find a common structure like

    src
    - App.js
    - components/
      - Nav.js
    - util/
      - hello.js
    

提交回复
热议问题