Skip to content

Instantly share code, notes, and snippets.

@oakfang
Last active June 6, 2019 07:01
Show Gist options
  • Save oakfang/7085c23f2a4582a128a7b000ce6744c4 to your computer and use it in GitHub Desktop.
Save oakfang/7085c23f2a4582a128a7b000ce6744c4 to your computer and use it in GitHub Desktop.
Black box function
function blackBox() {
var input = [0, 1, 2, 3, 4];
var results = [];
for (var i = 0; i < input.length; i++) {
results.push(
new Promise(function(resolve) {
setTimeout(function() {
resolve(i);
}, 200);
})
);
}
Promise.all(results).then(console.log);
}
import React, { Component } from "react";
class MyComponent extends Component {
state = {
valueLength: 0
};
componentDidMount() {
this.setState({ valueLength: this.props.value.length });
}
componentDidUpdate() {
this.setState({ valueLength: this.props.value.length });
}
render() {
const { valueLength } = this.state;
const { value } = this.props;
return <p>{value} is {valueLength} chars long</p>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment