Build web applications as easy as datarockets do π
yarn add dreactMaintain projects easier by applying reusage of everything. It's based on react-scripts (create react app) so you can use all of its features. We provide redux and styled-components configuration out of the box.
Content:
- Environment
- How to organize the project
- How to create and use store
- How to write sagas and containers related to store
- How to write tests
- How to integrate with Sentry
- Configuration
- CLI
- Deployment
The app is based on react-scripts. And book is based on storybook.
-
We use Pug to write render methods. See babel-plugin-transform-react-pug.
-
We provide
srcandUIaliases that point to./srcand./src/UI. -
I recommend to maintain
.env.sampleto always contain variables necessary for development and sync it viadreact env-synccommand. -
We use Jest + Enzyme. To make test writing easier, we configured it:
- The execcution of console.error is forbidden. This is mostly to prevent mismatching of prop types.
- Extended functioinality with jest-enzyme.
- Element's attribute
TESTIDin tests for easier selecting nodes n tests.
-
We added knobs, storysource and actions.
-
All
new Action()constructions inside/src/collections/*/actions.jswill be transformed intocreateActionexported bydreact/helper-actions. -
Each
.success()and.failure()actions trigger callbacks passed to.init()action.For example, when we initialize any action we can pass
onSuccessandonFailurecallbacks which will be triggered when corresponding success and failure actions are dispatched.const message = new Action({ init: data => data, // It's important to pass values }) const action = message.init({ text: 'hello', onSuccess: () => alert('Message has been sent'), onFailure: () => alert('Failed to send message'), }) dispatch(action) dispatch(message.success()) // Triggers `onSuccess` callback dispatch(message.failure()) // Triggers `onFailure` callback
-
We use eslint and stylelint to lint our files. Also we applied several custom rules there to reach our needs: we request named exports for collection stuff, we request using normal functions to define components, we require tests for each file in the app. Take a look at all rules.
-
We import React and styled components where they are used automatically.
The basic structure looks like this:
/config β to configure the project
/public
index.html
/src
/collections
...
store.js
/components
/containers
/forms
/lib
/pages
/services
/UI
AppRouter.js
index.js
routes.js
We create and export store in src/collections/store.js:
import makeStoreConfigurer from 'dreact/helper-store'
export default makeStoreConfigurer()Everything will be pulled from collections and added automatically. However you might need to extend it, so take a look at dreact/helper-store documentation.
Note: When we create or remove collectons we should restart the app.
To connect the store with the app we need to modify src/index.js:
import ReactDOM from 'react-dom'
import { Provider } from 'dreact/helper-store'
import AppRouter from 'src/AppRouter'
import configureStore from 'src/collections/store'
const store = configureStore()
ReactDOM.render(
pug`
Provider(store=store)
AppRouter
`,
document.getElementById('root'),
)So basically we need saga effects and some hooks to maintain everything related to store. For that we have dreact/helper-store.
import { effects, useDispatch, useSelector } from 'dreact/helper-store'There are no instructions. Earlier you've been told that we use enzyme for testing, but we built an enhancements on top of it, so to get an access to enzyme we should use dreact/helper-test.
Your test may look like this:
import { shallow } from 'dreact/helper-test'
import Component from '.'
it('is rendered', () => {
shallow(pug`Component Hello World`)
})Take a look at dreact/helper-test documentation.
How to integrate with Sentry
To implement the integration with sentry we need to set REACT_APP_SENTRY_DSN, REACT_APP_SENTRY_ENV environment variables. Once they are set, it will start sending reports to sentry.
-
The babel config will be based on this file and refined.
-
Specify a decorator for storybook. Usually used to add some wrappers around each story.
export default storyFn = pug` p I'll appear before each story = storyFn() `
-
Modify our internal eslint config to conform your needs.
-
Modify our internal stylelint config to conform your needs.
-
Setup jest to run tests on your terms.
-
Run the app in development mode
-
Run the book in development mode
-
Lint JavaScript files
-
Lint Typescript (.ts, .tsx) files
-
Lint styles in the project
-
Run tests in watch mode (on CI it will be run in a normal mode)
-
Synchronize
.env.samplefile with.env.local
-
It creates
/builddirectory that can be deployed to static server such as S3. -
It creates
/bookdirectory that can be deployed to static server such as S3.