forked from alleyinteractive/react-fundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NicoleA/exercises
Exercises 1-3
- Loading branch information
Showing
11 changed files
with
24,004 additions
and
48 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,25 +4,64 @@ | |
<body> | ||
<div id="root"></div> | ||
|
||
<!-- 💣 remove this line | ||
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script> | ||
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> | ||
💣 remove this line --> | ||
|
||
<script type="module"> | ||
const rootElement = document.getElementById('root') | ||
|
||
// You're going to re-implement this code using React! | ||
// 💣 So go ahead and delete this implementation (or comment it out for now) | ||
// These three lines are similar to React.createElement | ||
const element = document.createElement('div') | ||
element.textContent = 'Hello World' // 💰 in React, you set this with the "children" prop | ||
element.className = 'container' // 💰 in React, this is also called the "className" prop | ||
// const element = document.createElement('div') | ||
// element.textContent = 'Hello World' // 💰 in React, you set this with the "children" prop | ||
// element.className = 'container' // 💰 in React, this is also called the "className" prop | ||
// This is similar to ReactDOM.render | ||
rootElement.append(element) | ||
// rootElement.append(element) | ||
|
||
// 🐨 Please re-implement the regular document.createElement code above | ||
// with these React API calls | ||
// 💰 The example in the markdown file should be a good hint for you. | ||
const elementProps = {children: 'Hello world!'}; | ||
const elementType = 'div'; | ||
const reactElement = React.createElement(elementType, elementProps); | ||
ReactDOM.render(reactElement, rootElement); | ||
</script> | ||
|
||
<!-- EXTRA CREDIT --> | ||
<div id="root2"></div> | ||
|
||
<script type="module"> | ||
// See if you can figure out how to write the JavaScript + React code to generate this DOM output: | ||
// <body> | ||
// <div id="root"> | ||
// <div class="container"> | ||
// <span>Hello</span> | ||
// <span>World</span> | ||
// </div> | ||
// </div> | ||
// </body> | ||
|
||
// Get the root element where we want to append our content. | ||
const rootElement = document.getElementById('root2') | ||
|
||
// Create the span elements. | ||
const elProps = [ | ||
{key:1,children:'Hello'}, | ||
{key:2,children:'World'}, | ||
]; | ||
const spans = elProps.map((props) => | ||
React.createElement('span', props) | ||
); | ||
|
||
// Create the container div with the span elements as children. | ||
const containerElement = React.createElement('div', { | ||
className: 'container', | ||
children: spans, | ||
}); | ||
|
||
// Render the elements to the DOM. | ||
ReactDOM.render(containerElement, rootElement); | ||
|
||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,26 +6,63 @@ | |
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script> | ||
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script> | ||
|
||
<!-- 💣 remove this line | ||
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script> | ||
💣 remove this line --> | ||
|
||
<script type="module"> | ||
<script type="text/babel"> | ||
// 🐨 on the script tag above, change `type="module"` | ||
// to `type="text/babel"` so babel will compile this code for the browser to run. | ||
|
||
// 🐨 re-implement this using JSX! | ||
const element = React.createElement('div', { | ||
className: 'container', | ||
children: 'Hello World', | ||
}) | ||
// const element = React.createElement('div', { | ||
// className: 'container', | ||
// children: 'Hello World', | ||
// }); | ||
|
||
const element = <div className="container">Hello World</div>; | ||
|
||
// 💰 there are a few subtle differences between JSX and HTML. One such | ||
// difference is how you apply a class to an element in JSX is by using | ||
// `className` rather than `class`! | ||
// 📜 You can learn the differences between JSX and HTML syntax from the React docs here: | ||
// https://reactjs.org/docs/dom-elements.html#differences-in-attributes | ||
|
||
ReactDOM.render(element, document.getElementById('root')) | ||
ReactDOM.render(element, document.getElementById('root')); | ||
</script> | ||
|
||
<!-- EXTRA CREDIT --> | ||
<div id="root2"></div> | ||
|
||
<script type="text/babel"> | ||
// See if you can figure out how to extract the className ("container") and children | ||
// ("Hello World") to variables and interpolate them in the JSX. | ||
// const className = 'container' | ||
// const children = 'Hello World' | ||
// const element = <div className="hmmm">how do I make this work?</div> | ||
|
||
const className = 'container' | ||
const children = 'Hello World' | ||
const element = <div className={className}>{children}</div>; | ||
|
||
ReactDOM.render(element, document.getElementById('root2')); | ||
</script> | ||
|
||
<!-- EXTRA CREDIT --> | ||
<div id="root3"></div> | ||
|
||
<script type="text/babel"> | ||
// See if you can figure out how to make that work with JSX. | ||
const children = 'Hello World'; | ||
const className = 'container'; | ||
const props = {children, className}; | ||
//const element = <div /> // how do I apply props to this div? | ||
|
||
function Div(props) { | ||
const {children, ...other} = props; | ||
return <div {...other}>{children}</div>; | ||
} | ||
const element = <Div {...props}/>; | ||
|
||
ReactDOM.render(element, document.getElementById('root3')); | ||
|
||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters