react和react2_对本机React的介绍

react和react2

To work with React Native, you will need to have an understanding of JavaScript fundamentals.

要使用React Native,您需要了解JavaScript基础。

With React, you can make components using either classes or functions. Originally, class components were the only components that could have a state. But since the introduction of React’s Hooks API, you can add state and more to function components.

使用React,您可以使用类或函数来制作组件。 最初,类组件是唯一可以具有状态的组件。 但是自从引入React的Hooks API以来,您可以向功能组件添加状态和更多内容。

入门 (Getting Started)

The easiest way to get started is with Expo CLI. Expo is a set of tools built around React Native.

入门的最简单方法是使用Expo CLI。 Expo是围绕React Native构建的一组工具。

Make sure you have installed NodeJs and run the following command:

确保已安装NodeJ,并运行以下命令:

$ npm install -g expo-cli

To create a new project run:

要创建一个新的项目运行:

$ expo init newProject
$ cd newProject
$ npm start

This will start a development server for you.

这将为您启动开发服务器。

组件 (Components)

In Android development, you write views in Kotlin or Java; in iOS development, you use Swift or Objective-C. With React Native, you can invoke these views with JavaScript using React components.

在Android开发中,您可以使用Kotlin或Java编写视图; 在iOS开发中,您使用Swift或Objective-C。 使用React Native,您可以使用React组件通过JavaScript调用这些视图。

Core Components

核心组件

React Native has Core Components for everything from form controls to activity indicators. You can find them on the website of the react-native documentation.

React Native具有核心组件,可用于从表单控件到活动指示器的所有内容。 您可以在本机文档的网站上找到它们。

A few common components are:

一些常见的组件是:

  • View

    视图
  • Text

    文本
  • Image

    图片
  • ScrollView

    滚动视图
  • TextInput

    文字输入

Create your first component

创建您的第一个组件

Here is how you do it: First import ‘React’ and ‘React Native’:

操作方法如下:首先导入“React”和“React本机”:

import React from 'react'; 
import { Text } from 'react-native';

And you create a component like a regular javascript function:

然后创建类似于常规javascript函数的组件:

const Article = () => {};

You can add elements to these components, like text, images or inputs. Example:

您可以向这些组件添加元素,例如文本,图像或输入。 例:

const Article = () => {
return <Text>Hello, I am an Article!</Text>;
};

To be able to use this component, you need to export it:

为了能够使用此组件,您需要将其导出:

const Article = () => {
return <Text>Hello, I am an Article!</Text>;
};export default Article;

JSX (JSX)

React and React Native use JSX, a syntax that lets you write elements inside JavaScript. Any JavaScript expression will work between curly braces.

React和React Native使用JSX,该语法可让您在JavaScript中编写元素。 任何JavaScript表达式都可以在花括号之间使用。

For example:

例如:

const Article = () => {
const number = 1;
return <Text>Hello, I am an Article number {number}</Text>;
};export default Article;

道具 (Props)

Props is short for properties, these properties you can pass through components to, for example, display data.

道具是属性的缩写,这些属性可以通过组件传递,例如显示数据。

For example:

例如:

const Cat = (props) => {
return (
<View>
<Text>Hello, I am {props.name}!</Text>
</View>
);
}

(State)

A state is like personal storage for a component. The state is useful for handling data that changes over time or that comes from user interaction. State gives your component a memory.

状态就像组件的个人存储。 状态对于处理随时间变化或来自用户交互的数据很有用。 状态为您的组件提供了记忆。

Let’s see how that works:

让我们看看它是如何工作的:

First import state:

首次导入状态:

import React, { useState } from 'react';

Then add a new variable:

然后添加一个新变量:

const Cat = (props) => { const [isHungry, setIsHungry] = useState(true);  return (
<View>
<Text>Hello, I am {props.name} and I am {isHungry ? "hungry" : "full"}!!</Text>
</View>
);
}

文字输入 (Text Input)

The ‘<TextInput>’ element will let users be able to enter text to your application. You can store the text in a state for example.

“ <TextInput>”元素将使用户能够向您的应用程序输入文本。 例如,您可以以某种状态存储文本。

import React, { useState } from 'react';
import { Text, TextInput, View } from 'react-native';

Create your component:

创建您的组件:

const Cat = (props) => {
const [text, setText] = useState('');
return ( <View>
<TextInput
onChangeText={text => setText(text)}
/>
</View> );
}

When the ‘InputText’ element changes it replaces the value of the state with the value of the element.

当'InputText'元素更改时,它将状态值替换为该元素的值。

列表视图 (List Views)

The ‘FlatList’ component displays a scrolling list of changing, but similarly structured data. ‘FlatList’ works well for long lists of data, where the number of items might change over time.

“ FlatList”组件显示变化但结构类似的数据的滚动列表。 “ FlatList”适用于较长的数据列表,其中项目数可能会随时间变化。

Here an example of the ‘FlatList’:

以下是“ FlatList”的示例:

const FlatListBasics = () => {
return (
<View style={styles.container}>
<FlatList
data={[
{key: 'Devin'},
{key: 'Dan'},
{key: 'Dominic'},
{key: 'Jackson'},
{key: 'James'},
{key: 'Joel'},
{key: 'John'},
{key: 'Jillian'},
{key: 'Jimmy'},
{key: 'Julie'},
]}
renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
/>
</View>
);
}export default FlatListBasics;

结论 (Conclusion)

After this article, I hope you can write a simple React Native code now and have a good idea of what React Native is about.

在阅读完本文之后,希望您现在可以编写一个简单的React Native代码,并对React Native的内容有一个很好的了解。

翻译自: https://levelup.gitconnected.com/an-introduction-to-react-native-7a420b2346d4

react和react2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,首先我们需要先创建一个新的React项目。可以使用create-react-app工具快速创建一个新项目,具体步骤如下: 1. 打开终端,进入到你想要创建项目的目录中。 2. 运行以下命令安装create-react-app工具: ``` npm install -g create-react-app ``` 3. 运行以下命令创建新项目: ``` create-react-app my-app ``` 4. 进入新创建的my-app目录: ``` cd my-app ``` 5. 运行以下命令启动React应用: ``` npm start ``` 现在我们已经成功创建了一个新的React项目。 下面我们来参考之前的react页面代码和sys_dye表构建一个新的React页面。我们需要完成以下步骤: 1. 创建一个新的组件,比如叫做SysDyePage。 2. 在SysDyePage组件中使用React的生命周期函数,在组件挂载时从后端获取sys_dye表的数据。 3. 在组件中渲染表格,并将sys_dye表的数据填充到表格中。 下面是具体的代码实现: 1. 创建SysDyePage组件,可以在src目录下新建一个SysDyePage.js文件,代码如下: ```javascript import React, { Component } from 'react'; class SysDyePage extends Component { constructor(props) { super(props); this.state = { dyes: [], }; } componentDidMount() { fetch('/api/sys_dye') .then(response => response.json()) .then(data => { this.setState({ dyes: data }); }); } render() { return ( <div> <h1>Sys Dye Table</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Color</th> <th>Description</th> </tr> </thead> <tbody> {this.state.dyes.map(dye => ( <tr key={dye.id}> <td>{dye.id}</td> <td>{dye.name}</td> <td style={{ backgroundColor: dye.color }}>{dye.color}</td> <td>{dye.description}</td> </tr> ))} </tbody> </table> </div> ); } } export default SysDyePage; ``` 2. 在App.js中引入SysDyePage组件,并将其放置在路由中。可以在App.js中添加以下代码: ```javascript import React from 'react'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; import SysDyePage from './SysDyePage'; function App() { return ( <Router> <div> <nav> <ul> <li> <Link to="/">Home</Link> </li> <li> <Link to="/sys_dye">Sys Dye</Link> </li> </ul> </nav> <Route path="/sys_dye" component={SysDyePage} /> </div> </Router> ); } export default App; ``` 3. 启动React应用,在浏览器中访问http://localhost:3000/sys_dye即可看到sys_dye表的数据已经被渲染到页面中了。 以上就是参考之前的react页面代码和sys_dye表构建一个新react页面的具体步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值