Skip to content

Commit

Permalink
Reducers
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminreid committed Mar 1, 2017
1 parent f657fa8 commit fecf8dd
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ yo react-native:component MyComponent
yo react-native:component MyComponent --stateful
```

### `reducer`

An easy way to scaffold a reducer. It creates the reducer itself an example way to keep organise your associated actions.

```
yo react-native:reducer MyReducer
```

## Manual Notes

### After `react-native:base`
Expand Down
59 changes: 59 additions & 0 deletions generators/reducer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var Generator = require('yeoman-generator');
var path = require('path');

module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);

// name of the app based on the directory name generated by React Native
this.name = process.cwd().split(path.sep).pop();

this.argument('reducer', { type: String, required: true });
}

writing() {
var reducer = this.options.reducer;
var reducerConst = reducer.toUpperCase();
var reducerSlug = reducer.toLowerCase();

// create entry points for Android and iOS
this.fs.copyTpl(
this.templatePath('reducer.js'),
this.destinationPath(`App/Reducers/${reducer}.js`),
{
name: this.name,
reducer: reducer,
reducerConst: reducerConst,
reducerSlug: reducerSlug,
}
);

this.fs.copyTpl(
this.templatePath('actions.js'),
this.destinationPath(`App/Actions/${reducer}.js`),
{
name: this.name,
reducer: reducer,
reducerConst: reducerConst,
reducerSlug: reducerSlug,
}
);
}

end() {
var reducer = this.options.reducer;
var reducerConst = reducer.toUpperCase();
var reducerSlug = reducer.toLowerCase();

this.log('Please make sure to add this to your root reducer.');
this.log('App/Reducers/index.js');
this.log(`
import ${reducer} from '${this.name}/App/Reducers/${reducer}';
const reducers = combineReducers({
// ...other reducers
${reducerSlug}: ${reducer},
});
`);
}
};
6 changes: 6 additions & 0 deletions generators/reducer/templates/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @flow
export const <%= reducerConst %>_EXAMPLE = '<%= name %>/<%= reducerConst %>_EXAMPLE';

export const <%= reducerSlug %>Example = () => ({
type: <%= reducerConst %>_EXAMPLE,
});
21 changes: 21 additions & 0 deletions generators/reducer/templates/reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @flow
import { <%= reducerConst %>_EXAMPLE } from '<%= name %>/App/Actions/<%= reducer %>';

type State = {
};

type Action = {
type: string,
};

const initialState = {
};

const reducer = (state: State = initialState, action: Action): State => {
switch(action.type) {
default:
return state;
}
};

export default reducer;

0 comments on commit fecf8dd

Please sign in to comment.