Code Monkey home page Code Monkey logo

react's Introduction

readux-Saga的使用 //解决redux的异步请求

1.定义需要执行的异步请求

 function* apiGetUseinfo(action) {
    try {
        const res = yield call(login, action.params); //call方法相当执行 await login(action.params)
        yield put(getUseinfo(res.data))  //put方法将actions传递给reducers执行
        console.log(res)
    } catch {
        return
    }
}

2. 监听saga

    function* watch() {
        yield takeEvery(ASYNC_SET_USERINFO, apiGetUseinfo)
     }

3.redux 调用saga中间件

    import rootSaga from  "./saga"
    const sagaMiddleware=createSagaMiddleware()
    const store = createStore(
        Reducer,
        applyMiddleware(sagaMiddleware)
    )

    export default store

    sagaMiddleware.run(rootSaga)

react-redux的使用

1.reduct-action //返回action

    const asyncUseinfo = (userinfo) => {
    return {
        type: ASYNC_SET_USERINFO,
        userinfo
    }
}

2.reducers //reducers 中返回的值 必须返回跟原state中的键值一样 并且每个对应的action 必须有返回值

    const Reducer = (state = initState, action) => {
    // let count = state.count;
    switch (action.type) {
        case CHANEGE_COUTER:
            return {
                ...state,
                count:state.count+action.payload
            }
        case SET_USERINFO:{
            saveUserinfo(action.userinfo) //将用户信息存入stoarge
            return {
                ...state,
                userinfo:action.userinfo
            }
        }
        default:
            return state;
    }
}

3 actionsType //定义所有的actions

4 在组件中的使用 //将store中的值挂在到组件props中

  <button onClick={asyncClick.bind(this)}>saga</button>
  <h1>{this.props.count}</h1>

const mapStateToProps = (state) => ({
    count: state.count
})

const mapDispatchToProps = dispatch => ({
    onIncreaseClick: (num) => dispatch(changeCount(num)),
    asyncClick: () => dispatch(asyncCount())
})

const moduleHome = connect(
    mapStateToProps,
    mapDispatchToProps

)(Home);

5 全局连接store

    <Provider store={store}>
      <App />
    </Provider>

react's People

Contributors

hszuishuai avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.