如何在其他文件中使用状态值

时间:2017-07-14 12:05:31

标签: reactjs react-redux redux-saga

我想在组件文件中传递一个存储在我状态中的值“hash”。我希望在sagas文件中使用状态值“hash”作为url的参数。我怎样才能做到这一点。以下是mo代码文件。

传真文件

import { call, put, takeLatest } from 'redux-saga/effects';
import { REQUEST_DATA } from './constants';
import { requestDataSuccess, requestDataFailed } from './actions';
import { fetchTxs } from './api';

export function fetchDataFromServer() {
    return fetch('https://blockchain.info/rawtx/${hash}')
        .then((response) => response.json());
}

function* fetchData() {
    try {
        const data = yield call(fetchDataFromServer);
        yield put(requestDataSuccess(data));
    } catch (e) {
        yield put(requestDataFailed(e.message));
    }
}

// Individual exports for testing
export function* fetchSaga() {
    // See example in containers/HomePage/sagas.js
    yield takeLatest(REQUEST_DATA, fetchData)
}

// All sagas to be loaded
export default [
    fetchData,
];

1 个答案:

答案 0 :(得分:0)

您可以使用select方法。这是一份官方文件https://github.com/redux-saga/redux-saga/tree/master/docs/api#selectselector-args

使用它的例子

export const getSession = state => state.session;

// Saga
export function* saveChanges() {
  while(true) {
    yield take(SAVE_CHANGES);
    let session = yield select(getSession); // <-- get the session
    yield call(fetch, '/api/project', { body: session, method: 'PUT' });
    yield put({type: SAVE_SESSION_SUCCESS});
  }
}