Study the code below and choose the correct output that you will expect to see on the emulator.

4 phương ánCó giải thích
Đề bài
import React, { Component } from 'react';
import { Text, View } from 'react-native';

class Header extends Component {
  render() {
    return (
      <View>
        <Text>This is header!</Text>
      </View>
    );
  }
}

class Footer extends Component {
  render() {
    return (
      <View>
        <Text> This is footer!</Text>
      </View>
    );
  }
}

class MainScreen extends Component {
  render() {
    return (
      <View>
        <Header />
        <Footer />
      </View>
    );
  }
}

export default MainScreen;

Các phương án trả lời

Chọn một phương án
Lời giảiChọn phương án để mở
Đáp án đúng: "This is header! This is footer!". Lần theo mã: MainScreen render một View chứa lần lượt Header rồi Footer; Header render View bọc Text in "This is header!", Footer render View bọc Text in " This is footer!" (có dấu cách đầu). Vì View mặc định có flexDirection là column, hai dòng chữ hiện từ trên xuống theo đúng thứ tự khai báo, header trước, footer sau. "This is footer! This is header!" đảo thứ tự, sai vì React Native giữ thứ tự các con như trong JSX. "This is header!" thiếu phần footer dù Footer vẫn được render. "This is footer!" thiếu phần header dù Header được render trước. Các class component ở đây chỉ có render nên không có state hay điều kiện nào làm ẩn phần tử.

Cùng chương 2

Xuất hiện trong 1 bộ đề