简体   繁体   English

如何使用实用程序 function 来检测操作系统和浏览器的反应?

[英]How do I use a utility function to detect OS and Browser in react?

I'm attempting to use a utility function in order to detect the browser and the operating system on the login page in a react app.我正在尝试使用实用程序 function 来检测反应应用程序登录页面上的浏览器和操作系统。 Here is the method that I'm attempting to export into the Login component:这是我尝试导出到登录组件的方法:

//utils/platform.js //utils/platform.js

function getOperatingSystem(window) {
  let operatingSystem = 'Not known';
  if (window.navigator.appVersion.indexOf('Win') !== -1) { operatingSystem = 'Windows OS'; }
  if (window.navigator.appVersion.indexOf('Mac') !== -1) { operatingSystem = 'MacOS'; }
  if (window.navigator.appVersion.indexOf('X11') !== -1) { operatingSystem = 'UNIX OS'; }
  if (window.navigator.appVersion.indexOf('Linux') !== -1) { operatingSystem = 'Linux OS'; }

  return operatingSystem;
}

function getBrowser(window) {
  let currentBrowser = 'Not known';
  if (window.navigator.userAgent.indexOf('Chrome') !== -1) { currentBrowser = 'Google Chrome'; }
  else if (window.navigator.userAgent.indexOf('Firefox') !== -1) { currentBrowser = 'Mozilla Firefox'; }
  else if (window.navigator.userAgent.indexOf('MSIE') !== -1) { currentBrowser = 'Internet Exployer'; }
  else if (window.navigator.userAgent.indexOf('Edge') !== -1) { currentBrowser = 'Edge'; }
  else if (window.navigator.userAgent.indexOf('Safari') !== -1) { currentBrowser = 'Safari'; }
  else if (window.navigator.userAgent.indexOf('Opera') !== -1) { currentBrowser = 'Opera'; }
  else if (window.navigator.userAgent.indexOf('Opera') !== -1) { currentBrowser = 'YaBrowser'; }
  else { console.log('Others'); }

  return currentBrowser;
}

export const OS = (window) => { getOperatingSystem(window); };
export const currentBrowser = (window) => { getBrowser(window); };

The window object isn't available in the platform.js file so I'm attempting to add as an argument in the Login file. window object 在 platform.js 文件中不可用,因此我尝试将其作为参数添加到登录文件中。 Here is the login file below:下面是登录文件:

import { OS, currentBrowser } from '../../../utils/platform';

class Login extends BasePage {
  constructor(props) {
    super(props);
    this.state = {
      email: '',
      password: '',
      OS: '',
      browser: '',
    };
  }

componentDidMount() {
    this.checkNoSession();
    super.componentDidMount();
    console.log(OS(window));
    console.log(currentBrowser(window));
  }

Ultimately I'd like to set the state of OS and browser to be the value returned by the imported methods.最终我想将操作系统和浏览器的 state 设置为导入方法返回的值。 However currently when I'm console logging these I'm receiving 'undefined'.但是,目前当我在控制台记录这些时,我收到了“未定义”。

It's because your functions are not returning anything.这是因为您的函数没有返回任何内容。

export const OS = (window) => { 
  return getOperatingSystem(window); // <-- missing return
};
export const currentBrowser = (window) => { 
  return getBrowser(window); // <-- missing return
};

or you could implicitly return by dropping {}或者您可以通过删除{}隐式返回

export const OS = (window) => getOperatingSystem(window);
export const currentBrowser = (window) => getBrowser(window);

But if you do that you don't need the wrapper function at all但如果你这样做,你根本不需要包装器 function

export const OS = getOperatingSystem;
export const currentBrowser = getBrowser

And when you write it like that you might ask yourself why not just export the original functions当你这样写时,你可能会问自己为什么不直接导出原始函数

export function getOperatingSystem (window) { ... }
export function getBrowser (window) { ... }

You can use navigator object您可以使用导航器 object

Example:例子:

getOs = () => {
   const os = ['Windows', 'Linux', 'Mac']; // add your OS values
   return os.find(v=>navigator.appVersion.indexOf(v) >= 0);
}

For TypeScript I had to use modified version of Zouari answer:对于 TypeScript 我不得不使用修改版的 Zouari 答案:

const getOs = () => {
const os = ['Windows', 'Mac']; // add your OS values
return os.find(v => ((global as any).window?.navigator.platform.indexOf(v) >= 0));

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM