Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step 2 - Convert SelectUsers component #6

Open
wants to merge 14 commits into
base: 01-Filter
Choose a base branch
from
91 changes: 37 additions & 54 deletions src/SelectUsers.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,44 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { Select } from 'evergreen-ui';

export default class SelectUsers extends React.Component {
constructor(props) {
super(props);
const SelectUsers = ({ isLoading, userId, onChange, users }) => {
// const x = useMemo(()=> {return something}, [])
const [calculatedUsers, setCalculatedUsers] = useState([]);

this.state = {
calculatedUsers: [],
useEffect(() => {
const expensiveCalculationOnUsers = () => {
if (users) {
console.log('OMG this takes forever');
return users;
}
return [];
};
}

expensiveCalculationOnUsers() {
if (this.props.users) {
console.log('OMG this takes forever');
return this.props.users;
}
return [];
}
setCalculatedUsers(expensiveCalculationOnUsers());
}, [users]);

componentDidMount() {
this.setState({
calculatedUsers: this.expensiveCalculationOnUsers(),
});
}
return (
<Select
disabled={isLoading || !calculatedUsers.length}
onChange={e => onChange(e.currentTarget.name, e.currentTarget.value)}
value={userId}
name="userId"
id="userId"
>
{isLoading || !calculatedUsers.length ? (
<option>Loading users...</option>
) : (
<>
<option value="">Select User...</option>
{calculatedUsers.map(user => (
<option key={user.id} value={user.id}>
{user.name}
</option>
))}
</>
)}
</Select>
);
};

componentDidUpdate(prevProps) {
if (prevProps.users !== this.props.users) {
this.setState({
calculatedUsers: this.expensiveCalculationOnUsers(),
});
}
}

render() {
const { isLoading, userId, onChange } = this.props;

return (
<Select
disabled={isLoading || !this.state.calculatedUsers.length}
onChange={e => onChange(e.currentTarget.name, e.currentTarget.value)}
value={userId}
name="userId"
id="userId"
>
{isLoading || !this.state.calculatedUsers.length ? (
<option>Loading users...</option>
) : (
<>
<option value="">Select User...</option>
{this.state.calculatedUsers.map(user => (
<option key={user.id} value={user.id}>
{user.name}
</option>
))}
</>
)}
</Select>
);
}
}
export default SelectUsers;
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import App from './App';
import { hijackEffects } from 'stop-runaway-react-effects';

if (process.env.NODE_ENV !== 'production') {
hijackEffects();
hijackEffects({ callCount: 30, timeLimit: 6000 });
}

const rootElement = document.getElementById('root');
Expand Down