Skip to content

Commit d7c1631

Browse files
committed
Added Episode 2
1 parent 524b87e commit d7c1631

File tree

10 files changed

+183
-0
lines changed

10 files changed

+183
-0
lines changed

episode2/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Function as Child Components
2+
3+
ReactCasts, episode 2.
4+
5+
“Function as Child Component” is a pattern where the parent component accepts a function as child, instead of a nested React Node. This screencast shows how to create a FaCC and use it for making information available to wrapped components.
6+
7+
Screencast video:
8+
https://www.youtube.com/watch?v=WE3XAt9P8Ek
9+
10+
# Outline
11+
12+
- What are FaCC
13+
- Example: hypothetical use cases
14+
- Basic structure of a function as child component
15+
- Example: Scroll Position FaCC
16+
- Example: Refactor and Possible effects with the Scroll Position FaCC
17+
18+
# Build & Run Instructions
19+
20+
1. To build and run the code in this directory, ensure you have [npm](https://www.npmjs.com) installed
21+
22+
2. Install
23+
```
24+
npm install
25+
```
26+
27+
3. Start the application
28+
```
29+
npm start
30+
```

episode2/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "scrollPosition",
3+
"version": "0.1.0",
4+
"private": true,
5+
"devDependencies": {
6+
"react-scripts": "0.6.1"
7+
},
8+
"dependencies": {
9+
"react": "^15.3.2",
10+
"react-dom": "^15.3.2"
11+
},
12+
"scripts": {
13+
"start": "react-scripts start",
14+
"build": "react-scripts build",
15+
"test": "react-scripts test --env=jsdom",
16+
"eject": "react-scripts eject"
17+
}
18+
}

episode2/public/favicon.ico

24.3 KB
Binary file not shown.

episode2/public/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1">
6+
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
7+
<!--
8+
Notice the use of %PUBLIC_URL% in the tag above.
9+
It will be replaced with the URL of the `public` folder during the build.
10+
Only files inside the `public` folder can be referenced from the HTML.
11+
12+
Unlike "/favicon.ico" or "favico.ico", "%PUBLIC_URL%/favicon.ico" will
13+
work correctly both with client-side routing and a non-root public URL.
14+
Learn how to configure a non-root public URL by running `npm run build`.
15+
-->
16+
<title>React App</title>
17+
</head>
18+
<body>
19+
<div id="root"></div>
20+
<!--
21+
This HTML file is a template.
22+
If you open it directly in the browser, you will see an empty page.
23+
24+
You can add webfonts, meta tags, or analytics to this file.
25+
The build step will place the bundled scripts into the <body> tag.
26+
27+
To begin the development, run `npm start`.
28+
To create a production bundle, use `npm run build`.
29+
-->
30+
</body>
31+
</html>

episode2/src/App.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
h1 {
2+
font-size: 120px;
3+
}
4+
5+
.App {
6+
margin: 50px;
7+
}
8+
9+
.spacer {
10+
height: 1000px;
11+
}

episode2/src/App.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
import React, { Component } from 'react';
3+
import ScrollPos from './faac/ScrollPos';
4+
import './App.css';
5+
6+
class App extends Component {
7+
render() {
8+
return (
9+
<div className="App">
10+
<div className="spacer" />
11+
12+
<ScrollPos>
13+
{
14+
position => <h1>{'Awesome Text!!!'.substr(0,position*15)}</h1>
15+
}
16+
</ScrollPos>
17+
18+
<div className="spacer" />
19+
</div>
20+
);
21+
}
22+
}
23+
24+
export default App;

episode2/src/App.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
it('renders without crashing', () => {
6+
const div = document.createElement('div');
7+
ReactDOM.render(<App />, div);
8+
});

episode2/src/faac/ScrollPos.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { Component, PropTypes } from 'react';
2+
3+
class ScrollPos extends Component {
4+
5+
static proptypes = {
6+
children: PropTypes.func.isRequired,
7+
}
8+
9+
state = {
10+
position: null
11+
}
12+
13+
componentDidMount() {
14+
window.addEventListener('scroll', this.handleScroll);
15+
}
16+
17+
componentWillUnmount() {
18+
window.removeEventListener('scroll', this.handleScroll);
19+
}
20+
21+
getBounds = (element) => {
22+
if (this.bounds) return;
23+
this.bounds = element && element.getBoundingClientRect();
24+
}
25+
26+
handleScroll = (event) => {
27+
const windowSize = window.innerHeight;
28+
const scrollTop = event.srcElement.body.scrollTop;
29+
const visibleSpace = windowSize + (this.bounds.bottom - this.bounds.top);
30+
let visibleRatio = ((windowSize + scrollTop) - this.bounds.top + this.bounds.height)/visibleSpace;
31+
32+
if (visibleRatio < 0) visibleRatio = 0;
33+
if (visibleRatio > 1) visibleRatio = 1;
34+
35+
this.setState({position: visibleRatio})
36+
}
37+
38+
render() {
39+
return (
40+
<div ref={this.getBounds}>{
41+
this.props.children(this.state.position)
42+
}</div>
43+
)
44+
}
45+
}
46+
47+
export default ScrollPos;

episode2/src/index.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
font-family: sans-serif;
5+
}

episode2/src/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
import './index.css';
5+
6+
ReactDOM.render(
7+
<App />,
8+
document.getElementById('root')
9+
);

0 commit comments

Comments
 (0)