Last active
November 18, 2015 09:22
-
-
Save switchtrue/44486585a91752f5b7c5 to your computer and use it in GitHub Desktop.
Troubleshooting React (https://www.reddit.com/r/reactjs/comments/3t7vth/reactrouter_questions/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function enterRoom(id) { | |
return { | |
type: 'ENTER_ROOM', | |
payload: { | |
roomId: id | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import Router, {Route} from 'react-router'; | |
import App from './components/App'; | |
import reducer from './reducer'; | |
import {compose, createStore} from 'redux'; | |
import {Provider} from 'react-redux'; | |
import {LobbyContainer} from './components/Lobby'; | |
import {RoomContainer} from './components/Room'; | |
import {devTools, persistState} from 'redux-devtools'; | |
import {DevTools, DebugPanel, LogMonitor} from 'redux-devtools/lib/react'; | |
require('./css/reset.css'); | |
require('./css/style.css'); | |
const finalCreateStore = compose( | |
// Provides support for DevTools: | |
devTools(), | |
// Lets you write ?debug_session=<name> in address bar to persist debug sessions | |
persistState(window.location.href.match(/[?&]debug_session=([^&]+)\b/)) | |
)(createStore); | |
const store = finalCreateStore(reducer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {List, Map, fromJS} from 'immutable'; | |
function enterRoom(state, roomId) { | |
const availableRooms = state.get('availableRooms'); | |
var roomEntered = availableRooms.map(room => { | |
if (room.get('_id') == roomId) { | |
return room; | |
} | |
}); | |
var newState = state.set('currentRoom', roomEntered.get(0)); | |
return newState; | |
} | |
export default function(state = Map(), action) { | |
switch (action.type) { | |
case 'ENTER_ROOM': | |
return enterRoom(state, action.payload.roomId); | |
} | |
return state; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PureRenderMixin from 'react-addons-pure-render-mixin'; | |
import {connect} from 'react-redux'; | |
import * as actionCreators from '../actioncreators/Room'; | |
export const Room = React.createClass({ | |
mixins: [PureRenderMixin], | |
componentWillMount: function() { | |
this.props.enterRoom(this.props.params.id); | |
}, | |
render: function() { | |
return ( | |
<div> | |
<h1>Room:</h1> | |
{this.props.room.name} | |
</div> | |
); | |
} | |
}); | |
function mapStateToProps(state) { | |
return { | |
room: state.get('currentRoom') | |
}; | |
} | |
export const RoomContainer = connect( | |
mapStateToProps, | |
actionCreators | |
)(Room); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment