Skip to content

Commit

Permalink
git cherry-pick 812d8ac
Browse files Browse the repository at this point in the history
With respect I used @mglace 's react-starter-kit folk

* change deps versions (adopted higher ones)
* include minimum change of react-style-guide.md

Fixes #1715
  • Loading branch information
mglace authored and piglovesyou committed Nov 2, 2019
1 parent 096fb84 commit e644a61
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 51 deletions.
32 changes: 16 additions & 16 deletions docs/react-style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
### Table of Contents

* [Separate folder per UI component](#separate-folder-per-ui-component)
* [Prefer using functional components](#prefer-using-functional-components)
* [Use CSS Modules](#use-css-modules)
* [Use higher-order components](#use-higher-order-components)
- [Separate folder per UI component](#separate-folder-per-ui-component)
- [Prefer using functional components](#prefer-using-functional-components)
- [Use CSS Modules](#use-css-modules)
- [Use higher-order components](#use-higher-order-components)

### Separate folder per UI component

* Place each major UI component along with its resources in a separate folder\
- Place each major UI component along with its resources in a separate folder\
This will make it easier to find related resources for any particular UI element
(CSS, images, unit tests, localization files etc.). Removing such components during
refactorings should also be easy.
* Avoid having CSS, images and other resource files shared between multiple
- Avoid having CSS, images and other resource files shared between multiple
components.\
This will make your code more maintainable, easy to refactor.
* Add `package.json` file into each component's folder.\
- Add `package.json` file into each component's folder.\
This will allow to easily reference such components from other places in your code.\
Use `import Nav from '../Navigation'` instead of `import Nav from '../Navigation/Navigation.js'`

Expand All @@ -46,7 +46,7 @@ For more information google for

### Prefer using functional components

* Prefer using stateless functional components whenever possible.\
- Prefer using stateless functional components whenever possible.\
Components that don't use state are better to be written as simple pure functions.

```jsx
Expand All @@ -69,16 +69,16 @@ Navigation.propTypes = { items: PropTypes.array.isRequired };

### Use CSS Modules

* Use CSS Modules\
- Use CSS Modules\
This will allow using short CSS class names and at the same time avoid conflicts.
* Keep CSS simple and declarative. Avoid loops, mixins etc.
* Feel free to use variables in CSS via
- Keep CSS simple and declarative. Avoid loops, mixins etc.
- Feel free to use variables in CSS via
[precss](https://github.com/jonathantneal/precss) plugin for
[PostCSS](https://github.com/postcss/postcss)
* Prefer CSS class selectors instead of element and `id` selectors (see
- Prefer CSS class selectors instead of element and `id` selectors (see
[BEM](https://bem.info/))
* Avoid nested CSS selectors (see [BEM](https://bem.info/))
* When in doubt, use `.root { }` class name for the root elements of your
- Avoid nested CSS selectors (see [BEM](https://bem.info/))
- When in doubt, use `.root { }` class name for the root elements of your
components

```scss
Expand Down Expand Up @@ -127,7 +127,7 @@ Navigation.propTypes = { items: PropTypes.array.isRequired };
// Navigation.js
import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Navigation.scss';

function Navigation() {
Expand Down Expand Up @@ -156,7 +156,7 @@ export default withStyles(Navigation, s);

### Use higher-order components

* Use higher-order components (HOC) to extend existing React components.\
- Use higher-order components (HOC) to extend existing React components.\
Here is an example:

```js
Expand Down
23 changes: 13 additions & 10 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ import history from './history';
import { updateMeta } from './DOMUtils';
import router from './router';

// Enables critical path CSS rendering
// https://github.com/kriasoft/isomorphic-style-loader
const insertCss = (...styles) => {
// eslint-disable-next-line no-underscore-dangle
const removeCss = styles.map(x => x._insertCss());
return () => {
removeCss.forEach(f => f());
};
};

// Global (context) variables that can be easily accessed from any React component
// https://facebook.github.io/react/docs/context.html
const context = {
// Enables critical path CSS rendering
// https://github.com/kriasoft/isomorphic-style-loader
insertCss: (...styles) => {
// eslint-disable-next-line no-underscore-dangle
const removeCss = styles.map(x => x._insertCss());
return () => {
removeCss.forEach(f => f());
};
},
// Universal HTTP client
fetch: createFetch(fetch, {
baseUrl: window.App.apiUrl,
Expand Down Expand Up @@ -78,7 +79,9 @@ async function onLocationChange(location, action) {

const renderReactApp = isInitialRender ? ReactDOM.hydrate : ReactDOM.render;
appInstance = renderReactApp(
<App context={context}>{route.component}</App>,
<App context={context} insertCss={insertCss}>
{route.component}
</App>,
container,
() => {
if (isInitialRender) {
Expand Down
25 changes: 15 additions & 10 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import React from 'react';
import PropTypes from 'prop-types';

import StyleContext from 'isomorphic-style-loader/StyleContext';
import ApplicationContext from './ApplicationContext';

const ContextType = {
// Enables critical path CSS rendering
// https://github.com/kriasoft/isomorphic-style-loader
insertCss: PropTypes.func.isRequired,
// Universal HTTP client
fetch: PropTypes.func.isRequired,
pathname: PropTypes.string.isRequired,
Expand Down Expand Up @@ -44,20 +44,25 @@ const ContextType = {
*/
class App extends React.PureComponent {
static propTypes = {
// Enables critical path CSS rendering
// https://github.com/kriasoft/isomorphic-style-loader
insertCss: PropTypes.func.isRequired,
context: PropTypes.shape(ContextType).isRequired,
children: PropTypes.element.isRequired,
};

static childContextTypes = ContextType;

getChildContext() {
return this.props.context;
}

render() {
const { context, insertCss } = this.props;

// NOTE: If you need to add or modify header, footer etc. of the app,
// please do that inside the Layout component.
return React.Children.only(this.props.children);
return (
<StyleContext.Provider value={{ insertCss }}>
<ApplicationContext.Provider value={{ context }}>
{React.Children.only(this.props.children)}
</ApplicationContext.Provider>
</StyleContext.Provider>
);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/components/ApplicationContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';

const ApplicationContext = React.createContext({
fetch: () => {
throw new Error('Fetch method not initialized.');
},
});

export default ApplicationContext;
2 changes: 1 addition & 1 deletion src/components/Feedback/Feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Feedback.css';

class Feedback extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Footer.css';
import Link from '../Link';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import React from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Header.css';
import Link from '../Link';
import Navigation from '../Navigation';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Layout/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';

// external-global styles must be imported in your JS.
import normalizeCss from 'normalize.css';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import cx from 'classnames';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Navigation.css';
import Link from '../Link';

Expand Down
2 changes: 1 addition & 1 deletion src/components/Page/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Page.css';

class Page extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/admin/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Admin.css';

class Admin extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/contact/Contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Contact.css';

class Contact extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/error/ErrorPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './ErrorPage.css';

class ErrorPage extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Home.css';

class Home extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Login.css';

class Login extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/not-found/NotFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './NotFound.css';

class NotFound extends React.Component {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/register/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import React from 'react';
import PropTypes from 'prop-types';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import withStyles from 'isomorphic-style-loader/withStyles';
import s from './Register.css';

class Register extends React.Component {
Expand Down
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ app.get('*', async (req, res, next) => {
// Global (context) variables that can be easily accessed from any React component
// https://facebook.github.io/react/docs/context.html
const context = {
insertCss,
fetch,
// The twins below are wild, be careful!
pathname: req.path,
Expand All @@ -159,7 +158,9 @@ app.get('*', async (req, res, next) => {

const data = { ...route };
data.children = ReactDOM.renderToString(
<App context={context}>{route.component}</App>,
<App context={context} insertCss={insertCss}>
{route.component}
</App>,
);
data.styles = [{ id: 'css', cssText: [...css].join('') }];

Expand Down

0 comments on commit e644a61

Please sign in to comment.