Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learing redux saga #87

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions Counter.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React from 'react'
import { PropTypes } from 'prop-types';

const Counter = ({ value, onIncrement, onDecrement }) =>
<div>
<button onClick={onIncrement}>
Increment
</button>
{' '}
<button onClick={onDecrement}>
Decrement
</button>
<hr />
<div>
Clicked: {value} times
</div>
</div>
const Counter = ({ value, onIncrement, onDecrement, onIncrementAsync }) =>
<div>
<button onClick={onIncrementAsync}>
Increment after 2 second
</button>
<button style={{ color: "blue" }} onClick={onIncrement}>
Increment
</button>
{' '}
<button style={{ color: "red" }} onClick={onDecrement}>
Decrement
</button>
<hr />
<div>
Clicked: {value} times
</div>
</div>

Counter.propTypes = {
value: PropTypes.number.isRequired,
Expand Down
4 changes: 4 additions & 0 deletions hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function* helloWorld(){
console.log("this is our rudux sage application")
}

37 changes: 33 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,49 @@ import "@babel/polyfill"
import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, applyMiddleware } from 'redux'
// import createSagaMiddleware from "redux-saga"
import { put, take, takeEvery } from 'redux-saga/effects'

import Counter from './Counter'
import reducer from './reducers'
import createSagaMiddleware from 'redux-saga'
import { watchIncrementAsync } from "./sagas"

const store = createStore(reducer)
const sagaMiddleware = createSagaMiddleware()
import {helloWorld} from './hello'

const action = type => store.dispatch({type})
const sagaMiddlewar = createSagaMiddleware()
const store = createStore(reducer, applyMiddleware(sagaMiddlewar))
sagaMiddlewar.run(helloWorld);


const dispatch = type => store.dispatch({ type })

sagaMiddleware.run(watchIncrementAsync)

const delay = (ms)=>{ new Promise(res=> setTimeout(res,ms))}

// incrementAsync task to perform
export function* incrementAsync(){
yield delay(1000);
yield put({type: 'INCREMENT'})
}


// // Watcher
// INCREMENT_ASYNC
// export function* watchIncrementAsync(){
// yield takeEvery('INCREMENT_ASYNC', incrementAsync)
// }

function render() {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => action('INCREMENT')}
onDecrement={() => action('DECREMENT')} />,
onIncrement={() => dispatch('INCREMENT')}
onDecrement={() => dispatch('DECREMENT')}
onIncrementAsync={() => dispatch('INCREMENT_ASYNC')}
/>,
document.getElementById('root')
)
}
Expand Down
Loading