如何首先访问我的jwt,然后再连接到socketiomiddleware?

时间:2020-06-18 08:19:06

标签: react-native redux react-redux

console.ignoredYellowBox = ["Remote debugger"];
import { YellowBox } from "react-native";
YellowBox.ignoreWarnings([
  "Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`?"
]);
import React, { useState } from "react";
import { StyleSheet} from "react-native";
import * as Font from "expo-font";
import { AppLoading } from "expo";
import AppNavigation from "./navigation/AppNavigator";
import { createStore, applyMiddleware, combineReducers } from "redux";
import { Provider } from "react-redux";
import { setNavigator } from './navigationRef';
import ReduxThunk from 'redux-thunk';
import createSocketIoMiddleware from "redux-socket.io";
import {userReducer} from "./reducers/userReducer";
import {chatReducer} from "./reducers/chatReducer";

const store = createStore(combineReducers({userReducer, chatReducer}), applyMiddleware(ReduxThunk, socketIoMiddleware));

 let currentValue = store.getState().userReducer.token;
 const socket = io.connect('http://81.89.193.99:3001/chat', {
     query: {token: token} })
console.log("CuurentValue", currentValue);
const socketIoMiddleware = createSocketIoMiddleware(socket, "chat:");
const fetchFont = () => {
  return Font.loadAsync({ 
    "raleway-bold": require("./assets/fonts/Raleway-Bold.ttf"),
    ralewayBold: require("./assets/fonts/Raleway-Bold.ttf"),
    "roboto-regular": require("./assets/fonts/Roboto-Regular.ttf"),
    robotoRegular: require("./assets/fonts/Roboto-Regular.ttf"),
    Roboto_medium: require("./assets/fonts/Roboto-Regular.ttf"),
  });
};

export default function App() {
  const [fontLoaded, setFontLoaded] = useState(false);
  if (!fontLoaded) {
    return (
      <AppLoading
        startAsync={fetchFont}
        onFinish={() => setFontLoaded(true)}
        onError={(err) => {
          console.log(err);
        }}
      />
    );
  }
  return (
    <Provider store={store}>
      <AppNavigation ref = {(navigator) => {setNavigator(navigator)}} />
    </Provider>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

**我是redux的初学者,这是我关于redux的第一个项目。我想从商店中检索令牌。我尝试使用connect和store.getState()方法,但是没有运气。我想知道如何首先从商店中获取App.js中的jwt,然后连接到socket.io中间件

0 个答案:

没有答案