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

React counter - TabCorp - 22/02/2018 #125

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>