Câu hỏi phỏng vấn React Native
Câu hỏi

How do you style a component in react native?

Câu trả lời

Styling a component in React Native can be achieved through several methods, each with its own advantages and use cases. Here are the primary ways to style components in React Native:

1. Inline Styles

Inline styles are applied directly to the component using the style prop. This method is similar to inline CSS in web development but uses JavaScript objects.

javascript Copy
import React from 'react';
import { Text, View } from 'react-native';

const App = () => (
  <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'white' }}>
    <Text style={{ fontSize: 20, color: 'blue' }}>Hello, World!</Text>
  </View>
);

export default App;

2. StyleSheet

The StyleSheet API allows you to create a set of styles that can be reused across multiple components. This method helps keep the code organized and improves readability.

javascript Copy
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

const App = () => (
  <View style={styles.container}>
    <Text style={styles.text}>Hello, World!</Text>
  </View>
);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  },
  text: {
    fontSize: 20,
    color: 'blue',
  },
});

export default App;

3. Styled-Components

Styled-components is a library that allows you to write plain CSS in your JavaScript. It provides a more web-like styling experience and can be particularly useful for developers with a web development background.

javascript Copy
import React from 'react';
import styled from 'styled-components/native';

const Container = styled.View`
  flex: 1;
  justify-content: center;
  align-items: center;
  background-color: white;
`;

const StyledText = styled.Text`
  font-size: 20px;
  color: blue;
`;

const App = () => (
  <Container>
    <StyledText>Hello, World!</StyledText>
  </Container>
);

export default App;

4. Using External Stylesheets

You can also define styles in an external JavaScript file and import them into your component file. This method is useful for large projects where styles need to be shared across multiple components.

javascript Copy
// styles.js
import { StyleSheet } from 'react-native';

export const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'white',
  },
  ...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

What JavaScript engine does React native use?

senior

What is Component Driven Development (CDD)?

middle

What is flex dimension and how is it different from fixed dimension?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào