Skip to content

Instantly share code, notes, and snippets.

View NoamELB's full-sized avatar
🦄
magical

Noam Elboim NoamELB

🦄
magical
  • TLV
View GitHub Profile
@NoamELB
NoamELB / Tabs8.js
Last active December 22, 2018 14:00
static getDerivedStateFromProps(props, state) {
let newState = null;
if (props.value !== state.prevValue) {
newState = { prevValue: props.value };
if (props.value !== state.value) {
newState.value = props.value;
}
}
return newState;
}
@NoamELB
NoamELB / ShouldNotUpdateWithState.js
Last active September 25, 2018 11:00
component that should not update and will not update when not intended
class ShouldNotUpdate extends React.Component {
shouldComponentUpdate(nextProps, nextState) {
return this.props.type !== nextProps.type;
}
render() {
return 'I am rendering...';
}
}
@NoamELB
NoamELB / Tabs7.js
Last active December 22, 2018 15:18
static getDerivedStateFromProps(props, state) {
return makeControllable(props, state, 'value');
}
componentWillReceiveProps(nextProps) {
const { value } = nextProps;
if (this.props.value !== value && this.state.value !== value) {
this.setState({ value });
}
}
selectTab(e, value) {
this.props.onChange(e, value);
}
isSelected(tab) {
return tab.props.value === this.props.value;
}
selectTab(e, value) {
this.setState({ value });
this.props.onChange(e, value);
}
isSelected(tab) {
return tab.props.value === this.state.value;
}
@NoamELB
NoamELB / Tabs1.js
Last active December 7, 2017 15:16
getHeader(tabs) {
return tabs.map((tab, i) => {
const style = this.isSelected(tab) ? activeTabHeaderStyle : tabHeaderStyle;
return (
<span
key={tab.props.value}
onClick={e => this.selectTab(e, tab.props.value)}
style={i === 0 ? style : Object.assign({}, style, sideHeaderStyle)}
>
{tab.props.header}
<Tabs>
<Tab value="banana" header="Banana Header">Banana</Tab>
<Tab value="apple" header="Apple Header">Apple</Tab>
</Tabs>