@@ -2138,3 +2138,94 @@ function mapDispatchToProps(dispatch) {
21382138 } ;
21392139} ;
21402140export default connect ( mapStateToProps , mapDispatchToProps ) ( App ) ;
2141+
2142+
2143+
2144+ // SINGLE FILE BASIC REDUX
2145+ import React from 'react' ;
2146+ import { createStore } from "redux" ;
2147+ import { connect } from "react-redux" ;
2148+ import { Provider as ReduxProvider } from "react-redux" ;
2149+ // action type
2150+ const ADD = 'ADD' ;
2151+ // action
2152+ const addMessage = ( message ) => {
2153+ return { type : ADD , message : message }
2154+ } ;
2155+ // reducer
2156+ const messageReducer = ( state = [ ] , action ) => {
2157+ switch ( action . type ) {
2158+ case ADD :
2159+ return [
2160+ ...state ,
2161+ action . message
2162+ ] ;
2163+ default :
2164+ return state ;
2165+ }
2166+ } ;
2167+ // component
2168+ class Presentational extends React . Component {
2169+ constructor ( props ) {
2170+ super ( props ) ;
2171+ this . state = {
2172+ input : ''
2173+ }
2174+ this . handleChange = this . handleChange . bind ( this ) ;
2175+ this . submitMessage = this . submitMessage . bind ( this ) ;
2176+ }
2177+ handleChange ( event ) {
2178+ this . setState ( {
2179+ input : event . target . value
2180+ } ) ;
2181+ }
2182+ submitMessage ( ) {
2183+ this . props . submitNewMessage ( this . state . input ) ;
2184+ this . setState ( {
2185+ input : ''
2186+ } ) ;
2187+ }
2188+ render ( ) {
2189+ return (
2190+ < div >
2191+ < h2 > Type in a new Message:</ h2 >
2192+ < input
2193+ value = { this . state . input }
2194+ onChange = { this . handleChange } /> < br />
2195+ < button onClick = { this . submitMessage } > Submit</ button >
2196+ < ul >
2197+ { this . props . messages . map ( ( message , idx ) => {
2198+ return (
2199+ < li key = { idx } > { message } </ li >
2200+ )
2201+ } )
2202+ }
2203+ </ ul >
2204+ </ div >
2205+ ) ;
2206+ }
2207+ } ;
2208+ // getting state from redux into props
2209+ const mapStateToProps = ( state ) => {
2210+ return { messages : state }
2211+ } ;
2212+ // dispatching data to redux
2213+ const mapDispatchToProps = ( dispatch ) => {
2214+ return {
2215+ submitNewMessage : ( message ) => {
2216+ dispatch ( addMessage ( message ) )
2217+ }
2218+ }
2219+ } ;
2220+ // store
2221+ const store = createStore ( messageReducer ) ;
2222+ const Container = connect ( mapStateToProps , mapDispatchToProps ) ( Presentational ) ;
2223+ class AppWrapper extends React . Component {
2224+ render ( ) {
2225+ return (
2226+ < ReduxProvider store = { store } >
2227+ < Container />
2228+ </ ReduxProvider >
2229+ ) ;
2230+ }
2231+ } ;
0 commit comments