Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
React counter - TabCorp - 22/02/2018 (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioelizandro committed Feb 23, 2018
1 parent 1dfc4c2 commit 1745dd4
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
13 changes: 13 additions & 0 deletions counter-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
* **Format:** Mob Programming
* **Kata:** Working with React without webpack/babel
* **Where:** [TabCorp](https://www.tabcorp.com.au/)
* **When:** 22/02/2018

## Kata: React counter without webpack/babel

### Description:
Use React in a simple way as we used do in old times with jQuery, just linking the CDN file into our index.html.
We also did a simple redux implementation to lift the app's state to a centralized store.

### Tech Specifications:
- Uses React
84 changes: 84 additions & 0 deletions counter-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<html>

<body>
<div id="root"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script type="text/javascript">
const e = React.createElement;

const rootReducer = (state, action) => {
switch (action.type) {
case 'UP':
return {
counter: state.counter + 1
}
case 'DOWN':
return {
counter: state.counter - 1
}
default:
return state;
}
};


class Store {
constructor(initialState, reducer) {
this.state = initialState;
this.reducer = reducer;
this.listeners = [];
}

dispatch(action) {
this.state = this.reducer(this.state, action);
this.listeners.forEach(listener => listener());
}

subscribe(listener) {
this.listeners.push(listener);
}

getState() {
return this.state;
}
}

const store = new Store({ counter: 1 }, rootReducer);

const connect = (mapStateToProps) => {
return Component => {
return class extends React.Component {
constructor(props) {
super(props);

this.state = store.getState();

store.subscribe(() => {
this.setState(store.getState());
});
}

render() {
return e(Component, mapStateToProps(store.getState()));
}
};
};
};

const mapStateToProps = (state) => {
return { number: state.counter };
};

const Counter = connect(mapStateToProps)(props => {
const up = e('button', { onClick: () => store.dispatch({ type: 'UP' }) }, 'UP');
const down = e('button', { onClick: () => store.dispatch({ type: 'DOWN' }) }, 'DOWN');

return e('div', {}, up, props.number, down);
});

ReactDOM.render(e(Counter), document.getElementById('root'));
</script>
</body>

</html>

0 comments on commit 1745dd4

Please sign in to comment.