如何在组件外部发送redux动作?

时间:2018-06-10 20:27:38

标签: javascript reactjs react-native redux

我在redux移动应用中使用react-native 与后端的通信是通过使用socket.io的websockets进行的。

我组织项目的方式是我有一个文件(你可以称之为对象)来初始化套接字客户端。
处理程序已注册,我想将操作发送到reducers

socket.js

export function createSocket (url) {
  console.log(`connecting to ${url}`)
  // dispatch(actions.CONNECT)
  return io(url)
}

我如何dispatch来自任何react component之外的其他功能?

1 个答案:

答案 0 :(得分:5)

如果在启动时初始化套接字客户端,只需将redux存储传递给函数,然后使用 public class TableComboBoxByRow extends JFrame { List<TableCellEditor> editors = new ArrayList<TableCellEditor>(4); static List<MethodTrace2> methodtraces2= new ArrayList<MethodTrace2>(); public TableComboBoxByRow() throws SQLException { DatabaseReading2 db = new DatabaseReading2(); DatabaseReading2.MakePredictions(); methodtraces2= db.getMethodtraces2(); int j=0; String[] items1 = new String [methodtraces2.size()]; String[] items2 = new String [methodtraces2.size()]; String[] items3 = new String [methodtraces2.size()]; String[] items4 = new String [methodtraces2.size()]; Object[][] data = new Object[methodtraces2.size()][10000]; // Create the editors to be used for each row for(MethodTrace2 methodtrace: methodtraces2) { data[j][0]= methodtrace.MethodRepresentation.getMethodid(); data[j][1]= methodtrace.MethodRepresentation.getMethodname(); data[j][2]= methodtrace.Requirement.getID(); data[j][3]= methodtrace.Requirement.getRequirementName(); data[j][4]= methodtrace.ClassRepresentation.classid; data[j][5]= methodtrace.ClassRepresentation.classname; data[j][6]= methodtrace.gold; data[j][7]= methodtrace.subject; data[j][8]= methodtrace.goldpredictionCaller; data[j][9]= methodtrace.goldpredictionCallee; int i=0; items1 = new String[methodtrace.getCallersList().size()]; for(Method2Representation caller: methodtrace.getCallersList()) { items1[i]=caller.toString(); System.out.println(caller.toString()); i++; } // data[j][10]=items1; int k=0; items2 = new String[ methodtrace.getCalleesList().size()]; for(Method2Representation caller: methodtrace.getCalleesList()) { items2[k]=caller.toString(); System.out.println(caller.toString()); k++; } int r=0; items3 = new String[methodtrace.getCallersListExecuted().size()]; for(Method2Representation caller: methodtrace.getCallersListExecuted()) { items3[r]=caller.toString(); System.out.println(caller.toString()); r++; } int z=0; items4 = new String[10000]; for(Method2Representation caller: methodtrace.getCalleesListExecuted()) { items4[z]=caller.toString(); System.out.println(caller.toString()); z++; } JComboBox comboBox1 = new JComboBox( items1 ); DefaultCellEditor dce1 = new DefaultCellEditor( comboBox1 ); editors.add( dce1 ); JComboBox comboBox2 = new JComboBox( items2 ); DefaultCellEditor dce2 = new DefaultCellEditor( comboBox2 ); editors.add( dce2 ); JComboBox comboBox3 = new JComboBox( items3 ); DefaultCellEditor dce3 = new DefaultCellEditor( comboBox3 ); editors.add( dce3 ); JComboBox comboBox4 = new JComboBox( items4); DefaultCellEditor dce4 = new DefaultCellEditor( comboBox4 ); editors.add( dce4 ); j++; } String[] columnNames = {"MethodID","MethodName", "RequirementID", "RequirementName", "ClassID", "ClassName", "Gold", "Subject", "GoldPredictionCaller", "GoldPredictionCallee", "Callers", "CallersExecuted", "Callees", "CalleesExecuted"}; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model) { // Determine editor to be used by row public TableCellEditor getCellEditor(int row, int column) { int modelColumn = convertColumnIndexToModel( column ); if (modelColumn == 10 && row < methodtraces2.size()) return editors.get(0); else return super.getCellEditor(row, column); } }; table.getColumnModel().getColumn(10).setWidth(10000); table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); JScrollPane scrollPane = new JScrollPane( table ); getContentPane().add( scrollPane ); } class ComboBoxRenderer extends JComboBox implements TableCellRenderer { public ComboBoxRenderer() { setBorder(new EmptyBorder(0, 0, 0, 0)); } public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { removeAllItems(); addItem( value ); return this; } } public static void main(String[] args) throws SQLException { TableComboBoxByRow frame = new TableComboBoxByRow(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); } }

results = [for number in range(1,1001) max([divisor for divisor in range(1,10) if number % divisor == 0])]

如果不是在启动时,那么你的套接字创建应该由一些用户操作(或类似的东西)触发,在这种情况下你应该能够使用store.dispatch()中间件获得对调度函数的引用:

export function createSocket (url, store) {
  console.log(`connecting to ${url}`)
  store.dispatch(actions.CONNECT)
  return io(url)
}

有关详细信息,请参阅https://github.com/reduxjs/redux-thunk