Introduction to React Native and Basic 1.3 Styles
React Native components are styled using CSS-like style rules, but using JavaScript objects instead of text strings. Here is an example using styles
import React from 'react';
import {
StyleSheet, Text, View } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#fff',
},
text: {
fontSize: 20,
fontWeight: 'bold',
color: '#000',
},
});
const HelloWorld = () => {
return (
<View style={
styles.container}>
<Text style={
styles.text}>Hello, World!</Text>
</View>
);
};
export default HelloWorld;
In the above code, we defined a style object styles
called . This object contains two style rules: container
and text
. container
Rules are used to define a center-aligned container, and text
rules are used to define the style of text. be with
Note that when using styles, property names are camelCased rather than hyphen-separated.
Exercise 1.3:
- Create a component
MyButton
called with custom styles and aonPress
property that will print a message to the console when the button is pressed. A component's style should include at least a background color and a text color. - In a new component
MyView
, useMyView
the component and render a custom text and a defaultMyView
style in the component.
Reference answer:
1.
import React from 'react';
import {
StyleSheet, TouchableOpacity, Text } from 'react-native';
const MyButton = ({
onPress, label }) => {
return (
<TouchableOpacity style={
styles.button} onPress={
onPress}>
<Text style={
styles.label}>{
label}</Text>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
button: {
backgroundColor: '#4CAF50',
padding: 10,
borderRadius: 5,
},
label: {
color: '#fff',
fontWeight: 'bold',
textAlign: 'center',
},
});
export default MyButton;
import React, {
useState} from 'react';
import {
StyleSheet,
Text,
View,
Image,
TouchableOpacity,
TextInput,
} from 'react-native';
import CheckBox from '@react-native-community/checkbox';
const MyView = () => {
const [isSelected, setSelected] = useState(false);
return (
<View style={
styles.container}>
{
/* 添加一张图片 */}
<Image
source={
{
uri: 'https://reactnative.dev/img/tiny_logo.png'}}
style={
styles.image}
/>
{
/* 添加一个文本输入框 */}
<TextInput style={
styles.textInput} placeholder="请输入文本" />
{
/* 添加一个按钮 */}
<TouchableOpacity style={
styles.button}>
<Text style={
styles.buttonText}>确定</Text>
</TouchableOpacity>
{
/* 添加一个复选框 */}
<View style={
styles.checkBoxContainer}>
<CheckBox value={
isSelected} onValueChange={
setSelected} />
<Text style={
styles.checkBoxLabel}>我已阅读并同意条款</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
image: {
width: 50,
height: 50,
marginBottom: 20,
},
textInput: {
borderWidth: 1,
borderColor: '#999',
borderRadius: 4,
paddingVertical: 8,
paddingHorizontal: 12,
marginBottom: 20,
width: '80%',
},
button: {
backgroundColor: 'blue',
borderRadius: 4,
paddingVertical: 10,
paddingHorizontal: 20,
},
buttonText: {
color: '#fff',
fontWeight: 'bold',
textAlign: 'center',
},
checkBoxContainer: {
flexDirection: 'row',
alignItems: 'center',
},
checkBoxLabel: {
marginLeft: 8,
},
});
export default MyView;
The above is a simple React Native component that contains an image, a text input, a button and a checkbox.
First, we StyleSheet.create
created a stylesheet using the method. In styles, we define the style properties of each element of the component (including images, text input boxes, buttons, and check boxes).
Next, we defined a MyView
component named , and used the basic components in it React Native
to create the UI interface. Here, we import CheckBox
the component to support the functionality of the checkbox. In MyView
the component, we use useState
a hook to define a state variable isSelected
indicating whether the checkbox is checked or not. Then, we rendered an image, a text input, a button, and a checkbox inside the View, and bound isSelected
state variables and setSelected
functions to the checkbox.
Finally, we pass the stylesheet to View
the component as its style property to apply the defined styles.
useState
uses useState
functions to allow us to conveniently manage the state of components in function components, and does not need to write this.setState()
code similar to methods. It is React Hooks
an important part of , so that React
the function component has the same state management function as the class component.