Skip to content

Instantly share code, notes, and snippets.

View SASUKE40's full-sized avatar
Trying new things

Edward Evans SASUKE40

Trying new things
  • Seattle
  • 00:02 (UTC -08:00)
View GitHub Profile
const {deepEqual} = require('assert').strict;
const testCase1 = `AA00 = 10
AA01 = AA00 + AB00
AB00 = 15`;
const testCase2 = `AA00 = 10
AB00 = (AA00 + AA01) * 15
AA01 = 20 + AB00`;
TypeScript 22 hrs 49 mins █████████▉░░░░░░░░░░░ 47.4%
Other 20 hrs 16 mins ████████▊░░░░░░░░░░░░ 42.1%
JSX 2 hrs 4 mins ▉░░░░░░░░░░░░░░░░░░░░ 4.3%
JSON 1 hr 38 mins ▋░░░░░░░░░░░░░░░░░░░░ 3.4%
JavaScript 1 hr 7 mins ▍░░░░░░░░░░░░░░░░░░░░ 2.3%
import memoize from "memoize-one";
class Example extends Component {
// State only needs to hold the current filter text value:
state = { filterText: "" };
// Re-run the filter whenever the list array or filter text changes:
filter = memoize(
(list, filterText) => list.filter(item => item.text.includes(filterText))
);
// PureComponents only rerender if at least one state or prop value changes.
// Change is determined by doing a shallow comparison of state and prop keys.
class Example extends PureComponent {
// State only needs to hold the current filter text value:
state = {
filterText: ""
};
handleChange = event => {
this.setState({ filterText: event.target.value });
class Example extends Component {
state = {
filterText: "",
};
// *******************************************************
// NOTE: this example is NOT the recommended approach.
// See the examples below for our recommendations instead.
// *******************************************************
class EmailInput extends Component {
state = {
email: this.props.defaultEmail,
prevPropsUserID: this.props.userID
};
static getDerivedStateFromProps(props, state) {
// Any time the current user changes,
// Reset any parts of state that are tied to that user.
// In this simple example, that's just the email.
<EmailInput
defaultEmail={this.props.user.email}
key={this.props.user.id}
/>
class EmailInput extends Component {
state = { email: this.props.defaultEmail };
handleChange = event => {
this.setState({ email: event.target.value });
};
render() {
return <input onChange={this.handleChange} value={this.state.email} />;
}
function EmailInput(props) {
return <input onChange={props.onChange} value={props.email} />;
}
class EmailInput extends Component {
state = {
email: this.props.defaultEmail
};
resetEmailForNewUser(newEmail) {
this.setState({ email: newEmail });
}
// ...