连接动作以存储在组件外部?

时间:2018-10-24 16:11:49

标签: javascript reactjs redux

因此,假设我有一个商店,其中有一个public Mat matching (Mat matFrame, int viewMode, int resizeMode) { if (viewMode == VIEW_MODE_FEATURES) { initMatching(); if (!imageIsAnalyzed) { detectImageFromFile(); } detectFrame(matFrame, resizeMode); featureMatching(); outPutMat = drawingOutputMat(); } return outPutMat; private void initMatching () { detector = ORB.create(); descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB); matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING); } private void featureMatching () { matcher.knnMatch(descriptor1, descriptor2, matches, 2); //ratio test to get good matches if (matOfDMatch.toArray()[0].distance / matOfDMatch.toArray()[1].distance < 0.9) { good_matches.add(matOfDMatch.toArray()[0]); } //.... for(int i = 0; i<good_matches.size(); i++) { pts1.add(keyPoints1.toList().get(good_matches.get(i).queryIdx).pt); pts2.add(keyPoints2.toList().get(good_matches.get(i).trainIdx).pt); } //.... Calib3d.findHomography(pts1Mat, pts2Mat, Calib3d.RANSAC, 10, outPutMask, 2000, 0.995); //outPutMask contains zeros and ones indicating which matches are filtered better_matches = new LinkedList<DMatch>(); for (int i = 0; i < good_matches.size(); i++) { if (outPutMask.get(i , 0)[0] != 0.0) { better_matches.add(good_matches.get(i)); } } private void detectFrame (Mat matFrame, int resizeMode) { imgFromFrame = matFrame; Imgproc.resize(imgFromFrame, imgFromFrame, new Size(matFrame.width()/resizeMode, matFrame.height()/resizeMode)); descriptor2 = new Mat(); keyPoints2 = new MatOfKeyPoint(); detector.detect(imgFromFrame, keyPoints2); descriptor.compute(imgFromFrame, keyPoints2, descriptor2); } private Mat drawingOutputMat () { //Drawing Output outputImg = new Mat(); better_matches_mat = new MatOfDMatch(); better_matches_mat.fromList(better_matches); //this will draw all matches Features2d.drawMatches(imgFromFile, keyPoints1, imgFromFrame, keyPoints2, better_matches_mat, outputImg); //Instead of the drawing matches I will need some classification and some overlay on the output return outputImg; } 中间件。我创建了商店并将其导出为:

redux-thunk

我现在可以在应用程序中的任何位置访问它。但是,如果我想从任何地方调度动作怎么办?我宣布他们例如在import myOwnCreateStoreMethod from './redux/createStore'; export const store = myOwnCreateStoreMethod(); 中:

myAction.js

现在我可以导入它们,并像这样连接到我的商店/组件:

export const myAction = () => (dispatch, getState) =>
    dispatch({ type: 'SOME_TYPE', payload: ... })

我的问题是-如果我没有组件,但仍想发送声明为上述动作的动作怎么办?

2 个答案:

答案 0 :(得分:4)

您可以直接从store调度操作

import store from './myStore';
import { myAction } from './myAction';    

store.dispatch(myAction());

Redux本身就是一个库。

与React无关,它们只是基于React的单个事实来源和Redux一个全局存储(作为我们应用程序的状态)一起很好地协同工作。

您可以在每个JavaScript应用程序中使用redux。

答案 1 :(得分:0)

啊,@ Asaf Aviv写下他的简单答案后,就这么容易了。所以我是这样实现的:

import * as yourActions from './redux/actions/yourActions';
import { store } from './path/to/your/store';

const connectActions = (store, actions) => {
  const { dispatch } = store;
  return Object.keys(actions).reduce((acc, key) => {
    const action = props => dispatch(actions[key](props));
    acc[key] = action;
    return acc;
  }, {});
};

const connectedActions = connectActions(store, yourActions);