Procházet zdrojové kódy

first part of redux tutorial

Bernadette Elena Hammerle před 4 roky
rodič
revize
78d90217ac

+ 1 - 1
src/App.js

@@ -29,7 +29,7 @@ export class App extends React.Component{
     }
     
     hello(){
-      return <p>hello</p>
+      return "hello"
     }
     
     render(){

+ 4 - 2
src/index.js

@@ -2,6 +2,8 @@ import React from 'react';
 import ReactDOM from 'react-dom';
 import { App } from './App';
 import 'bootstrap/dist/css/bootstrap.min.css';
+import { Provider } from "react-redux";
+import store from "./js/store/index";
 
 
 export { default as Navigation } from "./Navigation";
@@ -11,8 +13,8 @@ export { default as About } from "./About";
 
 
 ReactDOM.render(
-    <div>
+    <Provider store={store}>
         <App title="App"/>
-    </div>,
+    </Provider>,
     document.getElementById('root')
 );

+ 25 - 0
src/js/actions/index.js

@@ -0,0 +1,25 @@
+ import { ADD_ARTICLE } from "../constants/action-types";
+ 
+ export function addArticle(payload) {
+   return { type: ADD_ARTICLE, payload }
+ };
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 

+ 21 - 0
src/js/constants/action-types.js

@@ -0,0 +1,21 @@
+export const ADD_ARTICLE = "ADD_ARTICLE"; 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 

+ 5 - 0
src/js/index.js

@@ -0,0 +1,5 @@
+import store from "../js/store/index";
+import { addArticle } from "../js/actions/index";
+
+window.store = store;
+window.addArticle = addArticle;

+ 16 - 0
src/js/reducers/index.js

@@ -0,0 +1,16 @@
+import { ADD_ARTICLE } from "../constants/action-types";
+
+const initialState = {
+  articles: []
+};
+
+function rootReducer(state = initialState, action) {
+  if (action.type === ADD_ARTICLE) {
+    return Object.assign({}, state, {
+      articles: state.articles.concat(action.payload)
+    });
+  }
+  return state;
+};
+
+export default rootReducer;

+ 6 - 0
src/js/store/index.js

@@ -0,0 +1,6 @@
+import { createStore } from "redux";
+import rootReducer from "../reducers/index";
+
+const store = createStore(rootReducer);
+
+export default store;