Rewritten to hooks

This commit is contained in:
2019-07-05 13:38:02 +02:00
parent a17faa9482
commit 41b4d3b115
12 changed files with 278 additions and 393 deletions

View File

@@ -1,38 +1,19 @@
import { Component } from 'react';
import { useEffect, useState } from 'react';
import log from '../../log';
class Log extends Component {
constructor() {
super();
this.state = {
logs: [],
};
this.setLogs = this.setLogs.bind(this);
}
const useLog = () => {
const [logs, setLogs] = useState([]);
useEffect(() => {
const update = (newLogs) => {
setLogs([...newLogs]);
}
log.listen(update);
componentDidMount() {
log.listen(this.setLogs);
}
return () => {
log.unlisten(update);
}
}, []);
return logs;
};
componentWillUnmount() {
log.unlisten(this.setLogs);
}
setLogs(logs) {
this.setState({
logs,
});
}
render() {
const {
children,
} = this.props;
const component = children(
this.state,
);
return component;
}
}
export default Log;
export default useLog;