简体   繁体   English

如何在React Native中注入JavaScript?

[英]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 ). 我假设你已经导入了一切必要( StyleSheetPlatformViewTextreact-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.

相关问题 如何在 WebView React Native 中注入 JavaScript - How to inject JavaScript in WebView React Native react-native-webview 如何注入 javascript? - react-native-webview how to inject javascript? 如何在反应中注入 javascript 变量? - How to inject javascript variable in react? 使用React Native进行Web抓取和注入JavaScript的最佳方法? - Best way to Web-scrape and inject JavaScript using React Native? 是否可以在不更新整个包的情况下在 React Native 中注入一些 javascript? - Is it possible to inject some javascript in React Native without updating the entire bundle? React JS如何在JSX对象中注入JavaScript - React js how to inject javascript in jsx object 注入 Javascript 函数在 Android React Native WebView 中不起作用,但在 iOS React Native 中工作正常 - Inject Javascript Function not working in Android React Native WebView but work fine in iOS React Native 如何将非静态的按需NativeModules注入React Native Android? - How to inject non static, on demand NativeModules into React Native Android? 如何注入javascript和修改原生android浏览器google搜索结果 - how to inject javascript and modify native android browser google search results MobX + React Native:注入商店的方式 - MobX + React Native : way to inject stores
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM