Tạo một component WeatherDisplay trong ReactJS
Trong bài tập này, mình sẽ thực hiện Bài 5 - "Weather Display", tạo một component WeatherDisplay để hiển thị thông tin thời tiết dựa trên một biến isSunny.
Weather Display trong ReactJS
Mình sẽ bắt đầu bằng việc tạo một ứng dụng React mới và thêm component WeatherDisplay vào đó.
Bước 1: Tạo một ứng dụng React mới
npx create-react-app react-weather-display cd react-weather-display
Bước 2: Tạo component WeatherDisplay
Trong thư mục src, tạo một file mới có tên là WeatherDisplay.js. File này sẽ chứa component WeatherDisplay.
WeatherDisplay.js
import React from 'react';
const WeatherDisplay = ({ isSunny }) => {
return <div>Today is {isSunny ? 'sunny' : 'cloudy'}.</div>;
};
export default WeatherDisplay;
Bước 3: Tạo file CSS cho WeatherDisplay (Optional)
Nếu cần thiết, bạn có thể tạo một file CSS để trang trí cho component WeatherDisplay. Trong thư mục src, tạo một file mới có tên là WeatherDisplay.css.
WeatherDisplay.css
.weather-display {
font-size: 18px;
margin-top: 20px;
}
.weather-display.sunny {
color: #ff9800; /* Màu cam */
}
.weather-display.cloudy {
color: #607d8b; /* Màu xám */
}
Bước 4: Sử dụng component WeatherDisplay trong App
Sửa nội dung file src/App.js để sử dụng component WeatherDisplay.
App.js
import React from 'react';
import WeatherDisplay from './WeatherDisplay';
import './WeatherDisplay.css'; // Import CSS file if created
function App() {
// Assume the value of isSunny is retrieved from somewhere
const isSunny = true;
return (
<div className="App">
<h1>React Weather Display Exercise</h1>
<WeatherDisplay isSunny={isSunny} />
</div>
);
}
export default App;
Bước 5: Chạy ứng dụng React
Quay lại terminal và chạy lệnh sau để khởi động ứng dụng React:
npm start
Mở trình duyệt và truy cập http://localhost:3000 để xem ứng dụng của bạn. Bạn sẽ thấy thông tin về thời tiết được hiển thị, với "Today is sunny!" nếu isSunny là true, và "Today is cloudy." nếu isSunny là false.

Kết bài:
Trong bài tập "Weather Display" này, mình đã tạo một component để hiển thị thông tin thời tiết dựa trên một biến isSunny. Điều này cho phép chúng ta điều khiển việc hiển thị nội dung dựa trên điều kiện. Hãy tiếp tục thực hành và tìm hiểu các tính năng khác của ReactJS để nâng cao kỹ năng lập trình của bạn!
Bài giải
-------------------- ######## --------------------
Câu hỏi thường gặp liên quan:
- Tạo một component SimpleConditionalRendering trong ReactJS
- Tạo một component UserAuthentication trong ReactJS
- Tạo một component RoleBasedAccessControl trong ReactJS
- Tạo Conditional Component trong ReactJS
- Tạo một component WeatherDisplay trong ReactJS
- Tạo một component ErrorMessage trong ReactJS
- Tạo một component ConditionalStyling trong ReactJS
- Tạo một component LoginForm trong ReactJS
- Tạo một component ShowHideElement trong ReactJS
- Tạo một component ConditionalRenderingWithList trong ReactJS
- Tạo một component SubscriptionPlan trong ReactJS
- Tạo một component OnlineOfflineStatus trong ReactJS
- Tạo một component ShoppingCart trong ReactJS
- Tạo một component ShowHideModal trong ReactJS
- Tạo một component ConditionalNavigation trong ReactJS

Các hàm xử lý chuỗi trong Javascript (cắt / tách / nối chuỗi ..)
Chia lấy phần dư / chia lấy phần nguyên trong javascript
Các cách khai báo biến trong Javascript
Các sự kiện (Event) trong Javascript
Hướng tạo thanh search bar bằng CSS
Hàm array.slice() trong Javascript
Tính tổng hai số bằng Javascript (cộng hai số)
Cách khai báo biến trong PHP, các loại biến thường gặp
Download và cài đặt Vertrigo Server
Thẻ li trong HTML
Thẻ article trong HTML5
Cấu trúc HTML5: Cách tạo template HTML5 đầu tiên
Cách dùng thẻ img trong HTML và các thuộc tính của img
Thẻ a trong HTML và các thuộc tính của thẻ a thường dùng