内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>javascript - Access redux without react - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access redux without react - Stack Overflow

programmeradmin0浏览0评论

I want access to the store (to the dispatch and observe) without the usage of React Components. I've been looking for a couple of hours, with no result.

This is the case. I've created the store in the App root:

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

let store = createStore();

ReactDOM.render( 
  <Provider store={store}>
    <App>
  </Provider>, 
  document.getElementById('root')
);

I'm happy using the connect function offered from react-redux when I need to add actions or listen to state changes in a ponent, but when I want to set some logics outside the ponent (mainly dispatch) i get stuck.

In short, I want to validate one field. I want to make a validation.js file where I can listen for changes from the store, run the validation logics, and then dispatch an action with the eventual error.

As far as the store, it is not global and I'm not using a React Component. Which is the way to get the store to listen for changes and to dispatch actions?

Thanks in advance.

I want access to the store (to the dispatch and observe) without the usage of React Components. I've been looking for a couple of hours, with no result.

This is the case. I've created the store in the App root:

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

let store = createStore();

ReactDOM.render( 
  <Provider store={store}>
    <App>
  </Provider>, 
  document.getElementById('root')
);

I'm happy using the connect function offered from react-redux when I need to add actions or listen to state changes in a ponent, but when I want to set some logics outside the ponent (mainly dispatch) i get stuck.

In short, I want to validate one field. I want to make a validation.js file where I can listen for changes from the store, run the validation logics, and then dispatch an action with the eventual error.

As far as the store, it is not global and I'm not using a React Component. Which is the way to get the store to listen for changes and to dispatch actions?

Thanks in advance.

Share Improve this question edited May 9, 2017 at 22:31 Don 4,20212 gold badges50 silver badges81 bronze badges asked Mar 16, 2016 at 11:20 Andrea ReginatoAndrea Reginato 1,3381 gold badge17 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

This is what I do (after creating my store):

export const dispatch = store.dispatch

Then you can call dispatch from anywhere in your code (although I pretty much just do it from event handlers).

If you use Redux-Saga to manage your flow-control, you only rarely need to use dispatch, aside from event handlers for direct user inputs, because it wraps dispatching in its put API.

I don't think you should be listening to changes directly from the store, but you would need it in order to dispatch actions.

Listening to changes

In redux the natural place for you to call this kind of logic would be from the action that dispatches the change in the field.

export function updateSomeField(value, field) {
    fieldValidator.validate()
    return { type: ActionTypes.UPDATE_SOME_FIELD, field: field, value: value }
}

fieldValidator can hold a reference to the store in order to get the state he needs in order to perform his logic with store.getState(), or get that data from the action as a parameter (with the help of async actions):

export function updateSomeField(value, field) {
    return (dispatch, getState) => {
        fieldValidator.validate(getState.myScreen.fields)
        dispatch({ type: ActionTypes.UPDATE_SOME_FIELD, field: field, value: value })
    }
}

Getting the store

As soon as you create it, you can just provide it to anyone who needs it. You can either do it by initializing a singleton

let store = createStore();
FieldValidator.setInstance(new FieldValidator(store));

// FieldValidator.js
class FieldValidator {
    ...
     static _instance = null;

    static getInstance() {
        return FieldValidator._instance;
    }

    static setInstance(fieldValidator) {
        FieldValidator._instance = fieldValidator;
    }
    ...
}

Or by injecting a member

let store = createStore();
fieldValidator.store = store;
发布评论

评论列表(0)

  1. 暂无评论