[英]react.js “this” is a reserved word
I have a two fold question. 我有两个问题。 I am trying to to write a component that checks if the user is on mobile or not, if they are mobile, the state isMobile switches to true (and visa versa if on desktop).
我正在尝试编写一个组件来检查用户是否在移动设备上,如果用户在移动设备上,则状态isMobile切换为true(如果在台式机上,则相反)。
I have gotten it where I can check for mobile and that works, but getting it to say "true or false" and also conquering the this is a reserved word is stumping me pretty bad. 我已经在可以检查手机的地方找到了它,并且可以正常工作,但是让它说“对还是错”,并且克服了这个保留字 , 这让我很不好。
Any help here is majorly appreciated, here is my code: 非常感谢这里的任何帮助,这是我的代码:
//this part will be imported via a component. Need to check and make sure how to update state via component.
const checkIfMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (
checkIfMobile.Android() ||
checkIfMobile.BlackBerry() ||
checkIfMobile.iOS() ||
checkIfMobile.Opera() ||
checkIfMobile.Windows()
);
}
};
//testing out a function to a get a boolean (true or false)
const trueOrFalse = () => {
console.log('hi', checkIfMobile);
};
export default class App extends Component {
constructor() {
super();
//the state "isMobile" would update the boolean changing the test when user is using mobile device
state = { isMobile: true };
}
render() {
//an if/else statement to see if it will detect mobile. It does however I have the problem of "this is a reserved word"
if (checkIfMobile.any()) {
console.log("it's mobile");
} else {
trueOrFalse();
}
const isMobile = { this.state.isMobile };
return (
<div>
<ChromePluginNotice />
<Banner isMobile={isMobile} />
<Content isMobile={isMobile} />
<Footer />
</div>
);
}
}
thank you in advance for your help 预先感谢您的帮助
Change 更改
const isMobile = { this.state.isMobile };
to 至
const { isMobile } = this.state;
Since you mobile check is synchronous, you can update the isMobile
state property in the constructor. 由于移动检查是同步的,因此可以在构造函数中更新
isMobile
状态属性。 In addition, const { isMobile } = this.state;
另外,
const { isMobile } = this.state;
is the correct way of getting isMobile
out of the state, and it will solve your this is a reserved word problem. 是使
isMobile
脱离状态的正确方法,它将解决您这是一个保留字的问题。
The following code should work, but I haven't tested it: 以下代码应该可以工作,但是我尚未对其进行测试:
export default class App extends Component {
constructor() {
super();
// set the isMobile prop in state
this.state = { isMobile: checkIfMobile.any() };
}
render() {
const { isMobile } = this.state; // destructure isMobile to variable
return (
<div>
<ChromePluginNotice />
<Banner isMobile={isMobile} />
<Content isMobile={isMobile} />
<Footer />
</div>
);
}
}
Note : instead of reinventing the wheel, and creating your own mobile detect, try to use an existing module, such as bowser . 注意 :与其重新发明轮子并创建自己的移动检测,不如尝试使用现有模块,例如bowser 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.