create-react-app
, and using the vschoolProducts.js
data file code below, create an application that lists the Product
components on the page, which is generated using the standard .map()
method Based on data from vschoolProducts.js
.key
attribute with a unique identifier, otherwise the system will issue a corresponding warning.App.js
: import React from "react" import productsData from "./vschoolProducts" function App() { return ( <div> </div> ) } export default App
vschoolProducts.js
file: const products = [ { id: "1", name: "Pencil", price: 1, description: "Perfect for those who can't remember things! 5/5 Highly recommend." }, { id: "2", name: "Housing", price: 0, description: "Housing provided for out-of-state students or those who can't commute" }, { id: "3", name: "Computer Rental", price: 300, description: "Don't have a computer? No problem!" }, { id: "4", name: "Coffee", price: 2, description: "Wake up!" }, { id: "5", name: "Snacks", price: 0, description: "Free snacks!" }, { id: "6", name: "Rubber Duckies", price: 3.50, description: "To help you solve your hardest coding problems." }, { id: "7", name: "Fidget Spinner", price: 21.99, description: "Because we like to pretend we're in high school." }, { id: "8", name: "Sticker Set", price: 14.99, description: "To prove to other devs you know a lot." } ] export default products
App.js
file: import React from "react" import Product from "./Product" import productsData from "./vschoolProducts" function App() { const productComponents = productsData.map(item => <Product key={item.id} product={item}/>) return ( <div> {productComponents} </div> ) } export default App
id
property of objects from the data file is not necessary to display on the screen. This property came in handy for us to set the key
attribute of instances of the Product
component created by the .map()
method.Product.js
file: import React from "react" function Product(props) { return ( <div> <h2><font color="#3AC1EF">{props.product.name}</font></h2> <p>{props.product.price.toLocaleString("en-US", { style: "currency", currency: "USD" })} - {props.product.description}</p> </div> ) } export default Product
props.product.price
, use the toLocaleString()
method, with which we format the amount of the product.todosData.js
data todosData.js
, the contents of which are shown below, create a list of TodoItem
components and output this list in the App
component. Note that you will need to modify the TodoItem
component TodoItem
so that it can display the properties passed to it.todosData.js
file: const todosData = [ { id: 1, text: "Take out the trash", completed: true }, { id: 2, text: "Grocery shopping", completed: false }, { id: 3, text: "Clean gecko tank", completed: false }, { id: 4, text: "Mow lawn", completed: true }, { id: 5, text: "Catch up on Arrested Development", completed: false } ] export default todosData
App.js
file: import React from "react" import TodoItem from "./TodoItem" import todosData from "./todosData" function App() { const todoItems = todosData.map(item => <TodoItem key={item.id} item={item}/>) return ( <div className="todo-list"> {todoItems} </div> ) } export default App
TodoItem.js
file: import React from "react" function TodoItem(props) { return ( <div className="todo-item"> <input type="checkbox" checked={props.item.completed}/> <p>{props.item.text}</p> </div> ) } export default TodoItem
props.item.completed
property set to true
will be set, the checkboxes for which the same property set to false
was used will be cleared. But if you click the checkbox on the application page, nothing will happen, as we have incorrectly configured the corresponding HTML element. In the console you can see a warning about this..map()
method. In addition, here we continued to work on the training application. Next time, let's talk about class-based components..map()
method to create sets of components?Source: https://habr.com/ru/post/437988/