mirror of
https://github.com/morten-olsen/react-native-debug-console.git
synced 2026-02-08 00:36:26 +01:00
Rewritten to hooks
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user