Skip to content

Commit

Permalink
Merge pull request #20 from jakemmarsh/react-classes
Browse files Browse the repository at this point in the history
convert to es6 classes, remove mixins
  • Loading branch information
jakemmarsh committed Nov 7, 2015
2 parents 93393bd + b12943f commit 5dea7e2
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 55 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ The ReactJS files are all located within `/app/js`, structured in the following
/components
- Footer.js (Simple, static footer component rendered on all pages.)
- Header.js (Simple, static header component rendered on all pages.)
/mixins
/pages
- HomePage.js (Example home page, serving as the default route.)
- NotFoundPage.js (Displayed any time the user requests a non-existent route.)
Expand Down
8 changes: 4 additions & 4 deletions __tests__/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ describe('App', function() {
);
});

it('#_onUserChange should set the error state if an error is received', function() {
it('#onUserChange should set the error state if an error is received', function() {
const err = { message: 'Test error message.' };
const app = TestUtils.renderIntoDocument(
<App params={{}} query={{}}>
<div />
</App>
);

app._onUserChange(err, null);
app.onUserChange(err, null);
app.state.error.should.eql(err);
});

it('#_onUserChange should set the user state and clear error if a new user is received', function() {
it('#onUserChange should set the user state and clear error if a new user is received', function() {
const user = TestHelpers.fixtures.user;
const app = TestUtils.renderIntoDocument(
<App params={{}} query={{}}>
<div />
</App>
);

app._onUserChange(null, user);
app.onUserChange(null, user);
app.state.currentUser.should.eql(user);
Should(app.state.error).be.null();
});
Expand Down
Empty file removed __tests__/mixins/.gitkeep
Empty file.
48 changes: 26 additions & 22 deletions app/js/App.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,58 @@
'use strict';

import React from 'react';
import {ListenerMixin} from 'reflux';

import CurrentUserActions from './actions/CurrentUserActions';
import CurrentUserStore from './stores/CurrentUserStore';
import Header from './components/Header';
import Footer from './components/Footer';

const App = React.createClass({
const propTypes = {
params: React.PropTypes.object,
query: React.PropTypes.object,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.object
])
};

mixins: [ListenerMixin],
class App extends React.Component {

propTypes: {
params: React.PropTypes.object,
query: React.PropTypes.object,
children: React.PropTypes.oneOfType([
React.PropTypes.array,
React.PropTypes.object
])
},

getInitialState() {
return {
constructor(props) {
super(props);
this.state = {
currentUser: {}
};
},
}

_onUserChange(err, user) {
onUserChange(err, user) {
if ( err ) {
this.setState({ error: err });
} else {
this.setState({ currentUser: user || {}, error: null });
}
},
}

componentWillMount() {
console.log('About to mount App');
},
}

componentDidMount() {
this.listenTo(CurrentUserStore, this._onUserChange);
this.unsubscribe = CurrentUserStore.listen(this.onUserChange);
CurrentUserActions.checkLoginStatus();
},
}

componentWillUnmount() {
this.unsubscribe();
}

renderChildren() {
return React.cloneElement(this.props.children, {
params: this.props.params,
query: this.props.query,
currentUser: this.state.currentUser
});
},
}

render() {
return (
Expand All @@ -66,6 +68,8 @@ const App = React.createClass({
);
}

});
}

App.propTypes = propTypes;

export default App;
8 changes: 6 additions & 2 deletions app/js/components/Footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import React from 'react';

const Footer = React.createClass({
class Footer extends React.Component{

constructor(props) {
super(props);
}

render() {
return (
Expand All @@ -14,6 +18,6 @@ const Footer = React.createClass({
);
}

});
}

export default Footer;
8 changes: 6 additions & 2 deletions app/js/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import React from 'react';

const Header = React.createClass({
class Header extends React.Component {

constructor(props) {
super(props);
}

render() {
return (
Expand All @@ -14,6 +18,6 @@ const Header = React.createClass({
);
}

});
}

export default Header;
Empty file removed app/js/mixins/.gitkeep
Empty file.
16 changes: 11 additions & 5 deletions app/js/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import React from 'react';
import {Link} from 'react-router';
import DocumentTitle from 'react-document-title';

const HomePage = React.createClass({
const propTypes = {
currentUser: React.PropTypes.object
};

propTypes: {
currentUser: React.PropTypes.object
},
class HomePage extends React.Component {

constructor(props) {
super(props);
}

render() {
return (
Expand All @@ -28,6 +32,8 @@ const HomePage = React.createClass({
);
}

});
}

HomePage.propTypes = propTypes;

export default HomePage;
16 changes: 11 additions & 5 deletions app/js/pages/NotFoundPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import React from 'react';
import DocumentTitle from 'react-document-title';

const NotFoundPage = React.createClass({
const propTypes = {
currentUser: React.PropTypes.object
};

propTypes: {
currentUser: React.PropTypes.object
},
class NotFoundPage extends React.Component {

constructor(props) {
super(props);
}

render() {
return (
Expand All @@ -21,6 +25,8 @@ const NotFoundPage = React.createClass({
);
}

});
}

NotFoundPage.propTypes = propTypes;

export default NotFoundPage;
30 changes: 18 additions & 12 deletions app/js/pages/SearchPage.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
'use strict';

import React from 'react';
import LinkedStateMixin from 'react-addons-linked-state-mixin';
import {Link} from 'react-router';
import DocumentTitle from 'react-document-title';

const SearchPage = React.createClass({
const propTypes = {
currentUser: React.PropTypes.object
};

mixins: [LinkedStateMixin],
class SearchPage extends React.Component {

propTypes: {
currentUser: React.PropTypes.object
},

getInitialState() {
return {
constructor(props) {
super(props);
this.state = {
query: ''
};
},
}

handleQueryChange(evt) {
this.setState({
query: evt.target.value
});
}

render() {
return (
Expand All @@ -29,7 +33,7 @@ const SearchPage = React.createClass({

<h2>Your query: <span ref="queryDisplay">{this.state.query}</span></h2>

<input type="text" valueLink={this.linkState('query')} ref="searchInput" />
<input type="text" onChange={this.handleQueryChange.bind(this)} ref="searchInput" />
</div>

<div>
Expand All @@ -41,6 +45,8 @@ const SearchPage = React.createClass({
);
}

});
}

SearchPage.propTypes = propTypes;

export default SearchPage;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-rocket-boilerplate",
"version": "2.0.0",
"version": "2.1.0",
"author": "Jake Marsh <[email protected]>",
"description": "Boilerplate using React, Browserify, SASS, and Gulp.",
"repository": {
Expand Down Expand Up @@ -47,7 +47,6 @@
"lodash": "^3.10.0",
"mocha": "^2.3.3",
"react": "^0.14.x",
"react-addons-linked-state-mixin": "^0.14.0",
"react-addons-test-utils": "^0.14.0",
"react-document-title": "^1.0.2",
"react-dom": "^0.14.0",
Expand Down

0 comments on commit 5dea7e2

Please sign in to comment.