Files
react-from-zero/05-properties.html
Sejas 9fa426903f Example of the map call (#27)
* added spaish translation

* added the result of a map call, to clarify where is the array, and how the map function works.

* fixed the word components
2018-07-18 15:19:01 +02:00

52 lines
1.5 KiB
HTML

<!doctype html>
<title>05 Properties - React From Zero</title>
<script src="https://unpkg.com/react@16.4.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// Components, like elements, can use properties, too
function MyComponent(props) {
return (
<div className={props.className}>
<h1>Hello</h1>
<h2>{props.customData}</h2>
</div>
)
}
var reactElement = <MyComponent className="abc" customData="world"/>
// Which also works with an object and the spread (...) operator
var props = {
className: "abc",
customData: "world",
}
reactElement = <MyComponent {...props}/>
// This allows components with dynamic content
var planets = ["earth", "mars", "venus"]
// If an array is used as a "child node" each child needs a unique key-property
// This map call will return an array of components, like this example:
// [
// <MyComponent className="myClass" customData={"earth"} key={0}/>,
// <MyComponent className="myClass" customData={"mars"} key={1}/>,
// <MyComponent className="myClass" customData={"venus"} key={2}/>
// ]
var elements = planets.map(function(planet, index) {
return <MyComponent className="myClass" customData={planet} key={index}/>
})
reactElement = <div>{elements}</div>
var renderTarget = document.getElementById("app")
ReactDOM.render(reactElement, renderTarget)
</script>