<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zhenting</title>
    <description>The latest articles on DEV Community by Zhenting (@zhenpanda).</description>
    <link>https://dev.to/zhenpanda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F19625%2F0c6ddce9-36b0-42be-9326-2052c0e84982.png</url>
      <title>DEV Community: Zhenting</title>
      <link>https://dev.to/zhenpanda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhenpanda"/>
    <language>en</language>
    <item>
      <title>React Redux-Sagas Starter Guide</title>
      <dc:creator>Zhenting</dc:creator>
      <pubDate>Fri, 27 Sep 2019 01:44:59 +0000</pubDate>
      <link>https://dev.to/zhenpanda/react-redux-sagas-starter-guide-2h6e</link>
      <guid>https://dev.to/zhenpanda/react-redux-sagas-starter-guide-2h6e</guid>
      <description>&lt;p&gt;Super simple setup for redux-saga in React. It's very basic but works so let's get started.&lt;/p&gt;

&lt;p&gt;~ Creating react app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app redux_project 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;~ Installing Redux Saga packages&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i axios react-redux redux redux-saga
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We gonna delete everything except App.js and index.js. Clean up our App.js so it's just displaying simple text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';

class App extends Component {    

  render() {
    return(
      &amp;lt;div&amp;gt;
        Hello World
      &amp;lt;/div&amp;gt;
    )
  }
}
export default App;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;At this point we gonna need to do a little setup. We need to create 4 folders inside of our react-app. Under the src folder. Create the folder [actions] [api] [reducers] [sagas]. Each of these folders will contain code that will be connected to our react-app.&lt;/p&gt;

&lt;p&gt;Let's create our actions in our actions folder called data.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// actions types is object with the
// key: GET_DATA_REQUEST -&amp;gt; value: describes the action
export const Types = {
    GET_DATA_REQUEST: 'get_data_request',
}

// function that returns an object literal 
export const getDataRequest = () =&amp;gt; ({
    type: Types.GET_DATA_REQUEST
})

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We are going to create our reducers. Go into our [reducers] folder and create these two files. data.js and index.js. So what is a redux? A redux a store a place that contains the state of our application. It's like state in react but with a lot more complexity and functions. But before we can create our Redux we have to first create Types and actions.  &lt;/p&gt;

&lt;p&gt;inside the [reducers] data.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Types} from '../actions/data';

// create initial state for reducers
const INIT_STATE = {
    test: "Hello world!"
}

// reducer function to transform state
export default function data(state = INIT_STATE, action) {
    return state
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next we have to fill out our index.js inside of our [reducers]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {combineReducers} from 'redux';
import DataReducer from './data';

export default combineReducers({
    data: DataReducer
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay even tho we see the code that says api call and payload we are not going to worry about that right now cause the Redux store doesn't have anything directly to do with api's it just stores data from api's. Let's connect our Redux to our App, at this point we are only worry about getting that INIT_STATE to render on our page. Go to our index.js page in [src] to connect our Redux.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';

import reducers from './reducers';
import { Provider } from 'react-redux';
import { createStore } from 'redux';

const store = createStore(reducers);

// pass our store into our Provider
ReactDOM.render(
    // Provider pass data into our App
    &amp;lt;Provider store={store}&amp;gt;
        &amp;lt;App /&amp;gt;
    &amp;lt;/Provider&amp;gt;
, document.getElementById('root'));

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have wrapped our application with Redux by use  and passing our Redux state as &lt;code&gt;store&lt;/code&gt;. App lives under our Provider and is now ready to be connected to Redux. Open App.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import { connect } from 'react-redux';

class App extends Component {    

  render() {
    return(
      &amp;lt;div&amp;gt;
        {this.props.data.test}
      &amp;lt;/div&amp;gt;
    )
  }
}

// redux providing state takeover
const mapStateToProps = (state) =&amp;gt; {
    console.log("App State -&amp;gt;", state);
    return {
      data: state.data
    }
}
export default connect(mapStateToProps, { })(App)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Redux Provider takes over our state and no longer allow us to change state, but now passes the data from Redux store into our application by using &lt;code&gt;props&lt;/code&gt; and because we have access to props we can render that to our page. It now say "Hello Redux!".&lt;/p&gt;

&lt;p&gt;Go inside the [actions] folder and create a file called data.js inside of the actions folder. Okay the pattern behind Redux is that try to think of the redux is a state with a giant (switch statement) filters out stuff and make sure that when it gets data in the form of an action "dispatch" it takes that specific "dispatch" and changes your application state is a specific way and gives your application a new state. So actions are special functions that creates an object to give to our Redux and by reading that object the Redux will know how to update our state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// actions types is object with the
// key: GET_DATA_REQUEST -&amp;gt; value: describes the action
export const Types = {
    GET_DATA_REQUEST: 'get_data_request',
    GET_DATA_SUCCESS: 'get_data_success',
}

// function that returns an object literal 
export const getDataRequest = () =&amp;gt; ({
    type: Types.GET_DATA_REQUEST
})
// key: payload will contain result of the api call
export const getDataSuccess = ({data}) =&amp;gt; ({
    type: Types.GET_DATA_SUCCESS,
    payload: { data }
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay now comes the tricky part...&lt;br&gt;
THe way we are going to use Sagas with Redux is our application is with generators. So generators are functions that contains state and is very similar to our closure in JS. Where a function when run creates it's private variables so that it could be used later and can't be accessed by the scope outside it's own closure. Generator takes that idea one step further and is designed in a way that when you write the generator code you decide how it will return data by using the &lt;code&gt;yield&lt;/code&gt; key word it returns some data but maintain it's state until a condition is met.&lt;/p&gt;

&lt;p&gt;What we are going to do is create two generator functions the &lt;code&gt;fetchData&lt;/code&gt; generator has the job of making the api calls in the future, which if successful we will then pass that data from the api response to our &lt;code&gt;watchGetchData&lt;/code&gt; generator and if &lt;code&gt;fetchData&lt;/code&gt; gives us the go ahead we will then pass that data to our Redux.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { takeEvery, fork, put } from 'redux-saga/effects';
import * as actions from '../actions/data';

// create a generator function
function* fetchData() {
    try {
        yield put(actions.getDataSuccess( {data: "Saga Data!"} ))
    }catch(e) {
        console.log(e);
    }
}
function* watchFetchData() {
    // create watcher of fetchData function
    yield takeEvery(actions.Types.GET_DATA_REQUEST, fetchData);
}

const DataSagas = [
    fork(watchFetchData)
];

export default DataSagas;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;make sure we fill in the code for index.js for [sagas] just like [reducers]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { all } from 'redux-saga/effects';
import DataSagas from './data';

// combine all sagas
export default function* rootSaga() {
    yield all([
        ...DataSagas,
    ]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;change our [reducers] data.js code to return the new data being passed into redux from our sagas.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// reducer function to transform state
export default function data(state = INIT_STATE, action) {
    switch(action.type) {
        case Types.GET_DATA_SUCCESS: {
            console.log("redux -&amp;gt; ", action.payload.data)    
            return {
                test: action.payload.data
            }
        }
        default: return state;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With our redux connected to our sagas we can now update our App.js code and see if our Sagas are working. We start the application by calling the getDataRequest action then our Sagas is called giving us the data back and passing that data back into our application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getDataRequest } from './actions/data'; 

class App extends Component {    
  componentDidMount() {
    this.props.getDataRequest();
  }

  render() {
    return(
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;
          {this.props.data}
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    )
  }
}

// redux providing state takeover
const mapStateToProps = (state) =&amp;gt; {
    console.log("App State -&amp;gt;", state);
    return {
      data: state.data.test
    }
}
export default connect(mapStateToProps, { getDataRequest })(App)

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The last step is to have our application make api calls from our saga generators.&lt;br&gt;
Inside the [api] folder we are going to create data.js. All we are doing is making a get request to our test dummy db.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';

// data api calls
export const getData = () =&amp;gt; {
    return axios.get('http://localhost:4000/comments')
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's add in the api call into our saga&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { takeEvery, call, fork, put } from 'redux-saga/effects';
import * as actions from '../actions/data';
import * as api from '../api/data';

// create a generator function
function* fetchData() {
    // try to make the api call
    try {
        // yield the api responsse into data
        const data = yield call(api.getData);
        yield put(actions.getDataSuccess( {data: data.data} ))
    }catch(e) {
        console.log(e);
    }
}
function* watchFetchData() {
    // create watcher of fetchData function
    yield takeEvery(actions.Types.GET_DATA_REQUEST, fetchData);
}

const DataSagas = [
    fork(watchFetchData)
];

export default DataSagas;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And also update our redux to return the data from our api call&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {Types} from '../actions/data';

// create initial state for reducers
const INIT_STATE = {
    test: "Hello Redux!"
}

// reducer function to transform state
export default function data(state = INIT_STATE, action) {
    switch(action.type) {
        case Types.GET_DATA_SUCCESS: {
            console.log("redux -&amp;gt; ", action.payload.data.test)    
            return {
                test: action.payload.data.test
            }
        }
        default: return state;
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Okay we come full circle, our application call an action and it dispatched to our saga to make an ajax call and give that that back once the ajax call is completed and return that data to our redux store and it gets render to our page.&lt;br&gt;
Last but not least let's post to our dummy db and get that data back once we have posted it.&lt;/p&gt;

&lt;p&gt;First we create new post actions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// actions types is object with the
// key: GET_DATA_REQUEST -&amp;gt; value: describes the action
export const Types = {
    GET_DATA_REQUEST: 'get_data_request',
    GET_DATA_SUCCESS: 'get_data_success',
    GET_POSTS_REQUEST: 'get_posts_request',
    GET_POSTS_SUCCESS: 'get_posts_success',
    CREATE_POST_REQUEST: 'create_post_request',
}

// function that returns an object literal 
export const getDataRequest = () =&amp;gt; ({
    type: Types.GET_DATA_REQUEST
})
// key: payload will contain result of the api call
export const getDataSuccess = ({data}) =&amp;gt; ({
    type: Types.GET_DATA_SUCCESS,
    payload: { data }
})

// reading the posts
export const getPostsRequest = () =&amp;gt; ({
    type: Types.GET_POSTS_REQUEST
})
export const getPostsSuccess = ({posts}) =&amp;gt; ({
    type: Types.GET_POSTS_SUCCESS,
    payload: { posts }
})
export const createPostRequest = ({post}) =&amp;gt; ({
    type: Types.CREATE_POST_REQUEST,
    payload: {post}
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Update our api routes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';

// data api calls
export const getData = () =&amp;gt; {
    return axios.get('http://localhost:4000/comments')
}
export const getPosts = () =&amp;gt; {
    return axios.get('http://localhost:4000/posts')
}
export const createPost = ({post}) =&amp;gt; {
    console.log("api call -&amp;gt;", post)
    let randomId = Math.random()*100;
    return axios.post('http://localhost:4000/posts',{"id":randomId, "msg": post + randomId})
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Update our sagas&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { takeEvery, call, fork, put } from 'redux-saga/effects';
import * as actions from '../actions/data';
import * as api from '../api/data';

// create a generator function
function* fetchData() {
    // try to make the api call
    try {
        // yield the api responsse into data
        const data = yield call(api.getData);
        yield put(actions.getDataSuccess( {data: data.data} ))
    }catch(e) {
        console.log(e);
    }
}
function* watchFetchData() {
    // create watcher of fetchData function
    yield takeEvery(actions.Types.GET_DATA_REQUEST, fetchData);
}

function* fetchPosts() {
    try {
        const posts = yield call(api.getPosts);
        console.log(posts);
        yield put(actions.getPostsSuccess( {posts: posts.data} ))
    }catch(e) {
        console.log(e);
    }
}
function* watchFetchPosts() {
    yield takeEvery(actions.Types.GET_POSTS_REQUEST, fetchPosts);
}

function* createPost(action) {
    try {
        yield call(api.createPost, {post: action.payload.post});
        const posts = yield call(api.getPosts);
        yield put(actions.getPostsSuccess( {posts: posts.data} ))
    }catch(e) {
        console.log(e);
    }
}
function* watchCreatePost() {
    yield takeEvery(actions.Types.CREATE_POST_REQUEST, createPost);
}


const DataSagas = [
    fork(watchFetchData),
    fork(watchFetchPosts),
    fork(watchCreatePost)
];

export default DataSagas;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Last but not least update our App.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getDataRequest, getPostsRequest, createPostRequest } from './actions/data'; 

class App extends Component {    
  componentDidMount() {
    this.props.getDataRequest();
    this.props.getPostsRequest();
  }

  render() {
    return(
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;
          {this.props.data}
          {this.props.posts.map((e,i) =&amp;gt; {
            return(
              &amp;lt;div key={i}&amp;gt;{e.msg}&amp;lt;/div&amp;gt;
            )
          })}
        &amp;lt;/h1&amp;gt;

        &amp;lt;button onClick={() =&amp;gt; this.props.createPostRequest({"post": "Random Num Post - "})}&amp;gt;CLick to Add POst&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    )
  }
}

// redux providing state takeover
const mapStateToProps = (state) =&amp;gt; {
    console.log("App State -&amp;gt;", state);
    return {
      data: state.data.test,
      posts: state.data.posts
    }
}
export default connect(mapStateToProps, { getDataRequest, getPostsRequest, createPostRequest })(App)  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And Click that button we are good to go!&lt;/p&gt;

</description>
      <category>reactreduxsagas</category>
    </item>
    <item>
      <title>Hi, I'm Zhenting</title>
      <dc:creator>Zhenting</dc:creator>
      <pubDate>Tue, 23 May 2017 18:28:26 +0000</pubDate>
      <link>https://dev.to/zhenpanda/hi-im-zhenting</link>
      <guid>https://dev.to/zhenpanda/hi-im-zhenting</guid>
      <description>&lt;p&gt;I have been coding for 2 years.&lt;/p&gt;

&lt;p&gt;You can find me on GitHub as &lt;a href="https://github.com/zhenpanda" rel="noopener noreferrer"&gt;zhenpanda&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I live in Nashville.&lt;/p&gt;

&lt;p&gt;I work for Hashed Health&lt;/p&gt;

&lt;p&gt;I mostly program in these languages: JavaScript, CSS, Ruby.&lt;/p&gt;

&lt;p&gt;I am currently learning more about ETH Solidity.&lt;/p&gt;

&lt;p&gt;Nice to meet you.&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
