[英]How to inject JavaScript in React Native?
I am developing a react native app. 我正在开发一个本机应用程序。 I am super confused that how to inject javascript code in react native.
我超级困惑如何在响应本机中注入javascript代码。 Though I am struggling somehow to work with react native but the thing is sometimes I need to inject some javascript code in my react native or say the homescreen.js type files, but I somehow end up into a syntax error.
尽管我在以某种方式努力使用react native,但是有时候我还是需要在我的react native中注入一些javascript代码,或者说homescreen.js类型的文件,但是我最终还是遇到了语法错误。 For example:
例如:
I am trying to implement this in a stylesheet. 我正在尝试在样式表中实现这一点。
const styles = Stylesheet.create({
//some styles
viewStyle:{
Platform.select({
ios:{
marginTop:2,
},
android:{
marginTop:3
}
});
}
//some styles
});
But it just does not work! 但这只是行不通! it doesn't work even when I apply
...Platform
instead of Platform
. 即使我应用
...Platform
而不是Platform
它也不起作用。 **Note: I have import {Platform} from 'react-native'
**注意:我已经
import {Platform} from 'react-native'
My version is 0.57.something, but what I want to know is what is the syntax to implement javascript in these files. 我的版本是0.57.something,但是我想知道的是在这些文件中实现javascript的语法是什么。 Just like for example in PHP we have the following:
就像在PHP中一样,我们有以下内容:
<?php
//your logic
for(something){
?>
<View></View>
<?php
}
?>
Similarly for other languages we have something like the <% %>
, {{ }}
tags but what should I use in React Native. 类似地,对于其他语言,我们有类似
<% %>
, {{ }}
标签的东西,但是我应该在React Native中使用什么。 I always end up with a syntax error. 我总是以语法错误结尾。
So my questions are, can anyone help me understand what the restriction is within React Native and where I can use and should not use tags? 所以我的问题是,谁能帮助我了解React Native的限制是什么?我可以在哪里使用和不应该使用标签? And if tags are available what are they?
如果标签可用,它们是什么?
I am super confused and was unable to find a similar question like this anywhere on Stack Overflow. 我非常困惑,无法在Stack Overflow的任何地方找到类似的问题。 I'd appreciate anyone's inputs, the more answers the more it will be helpful.
我会很感激任何人的投入,答案越多,对您的帮助就会越大。
So I'm not sure what kind of structure you have set up but I'm going to assume you have a single file (lets call it homescreen.js
) and you want to define styles for a component. 因此,我不确定您设置了哪种类型的结构,但我将假设您只有一个文件(我们称它为
homescreen.js
),并且您想为组件定义样式。 I'm assuming you have already imported everything that's necessary ( StyleSheet
, Platform
, View
and Text
from react-native
, and the default export React
from react
). 我假设你已经导入了一切必要(
StyleSheet
, Platform
, View
和Text
从react-native
,默认出口React
,从react
)。
So first, let's make the stylesheet. 首先,让我们制作样式表。 You almost have it correct above, you need the following:
您几乎在上面正确无误,您需要以下内容:
const styles = Stylesheet.create({
viewStyle: Platform.select({
ios:{
marginTop:2,
},
android:{
marginTop:3
}
})
})
(You mention above that you're getting a syntax error, this is probably due to the semicolon in your stylesheet object.) (上面提到,您收到语法错误,这可能是由于样式表对象中的分号引起的。)
Now we need to create the React class for your HomeScreen component. 现在我们需要为您的HomeScreen组件创建React类。 Again, not sure of the context but you should be able to pull out the bits you need.
同样,不确定上下文,但是您应该能够提取所需的位。
class HomeScreen extends React.Component {
render() {
return (
<View style={styles.viewStyle}>
<Text>I should have marginTop 2 on iOS and marginTop 3 on Android.</Text>
</View>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.