This page collects all about IT, software and computers
Container Komponenten in React
Ein React-Pattern, das im Coding von React Frontends Berücksichtigung finden sollte, ist das Container-Komponenten-Pattern.
Die Idee ist einfach:
Ein Container ruft Daten ab und rendert dann die entsprechende Unterkomponente. So simpel.
"Entsprechend" bedeutet eine Komponente mit demselben Namensbestandteil:
GuineaPigsContainer => GuineaPigs
AnimalsContainer => Animals
EventListContainer => EventList
Konzept verstanden ?
Warum Container?
Angenommen, du hast eine Komponente, die Kommentare anzeigt. Da du noch nichts vom Container-Komponenten-Pattern gehört hast, platzierst du deinen Code so, dass alles an einem Ort ist:
class CommentList extends React.Component { this.state = { comments: [] }; componentDidMount() { fetchSomeComments(comments => this.setState({ comments: comments })); } render() { return ( <ul> {this.state.comments.map(c => ( <li>{c.body}—{c.author}</li> ))} </ul> ); } }
Deine Komponente ist sowohl für das Abrufen als auch für das Präsentieren von Daten verantwortlich. Daran ist nichts „Falsches“, aber einige Vorteile von React werden damit nicht nutzbar, zB:
Wiederverwendbarkeit
CommentList kann nur unter genau den gleichen Umständen wiederverwendet werden.
Datenstruktur
Die Markup-Komponenten sollten die Erwartungen an die benötigten Daten angeben. PropTypes sind dafür optimal.
Unsere Komponente hat eine Erwartung an die Datenstruktur, kann diese jedoch nicht ausdrücken. Wenn sich der json-Endpunkt ändert, wird der Datenfluß zur Komponente unterbrochen (silent).
Noch einmal. Diesmal mit einem Container
Lass uns zunächst den Datenabruf in eine Containerkomponente verlagern:
class CommentListContainer extends React.Component { state = { comments: [] }; componentDidMount() { fetchSomeComments(comments => this.setState({ comments: comments })); } render() { return <CommentList comments={this.state.comments} />; } }
Lass uns nun CommentList überarbeiten, um comments als prop zu verwenden:
const CommentList = props =>
<ul>
{props.comments.map(c => (
<li>{c.body}—{c.author}</li>
))}
</ul>
Also, was haben wir damit erreicht ?
Tatsächlich eine ganze Menge....
- Wir haben das Abrufen und Rendern von Daten getrennt.
- Wir haben unsere CommentList-Komponente wiederverwendbar gemacht.
- Wir haben CommentList die Möglichkeit gegeben, PropTypes festzulegen und damit ggf. auch im Fehlerfall "laut" zu scheitern.
Containerkomponenten können die Struktur und Lesbarkeit von React-Code erheblich verbessern und sie erleichtern das Lesen des Komponenten-Codes.
Probiere es aus !
Click here to read moreMQTT server mosquitto on Raspberry Smarthome server does not start
Problem
After a few days of absence, I was unfortunately faced with some problems on my Raspberry smart home server.
Among other things, the Mosquitto MQTT server, which is supposed to receive and process messages from the various iot devices, was not available.
Analysis
After analyzing the process overview with
ps -A | grep mosq
unfortunately no output was shown, i.e. the process had obviously not started.
The manual start with
sudo mosquitto
then led to success. The MQTT server was running and also receiving or processing all messages. Only the service to the Mosquitto caused problems.
After completing the manually started process, another attempt was made with
sudo systemctl start mosquitto.service
then start the service. The subsequent query with
sudo systemctl status mosquitto.service
initially provided a promising answer that the service had started successfully.
Unfortunately not quite successful, there is still no entry for mosquitto in the process overview, and the MQTT message processing did not work either.
Reason
After some troubleshooting, the problem was solved. The cause was an apparently corrupt mosquitto.db that is used by the mosquitto service to cache messages.
Solution
This problem can be easily solved by deleting the corresponding file in the directory / var / lib / mosquitto using:
sudo rm mosquitto.db
If then restarted with
sudo systemctl restart mosquitto.service
mosquitto works fine. The service is automatically restarted even after a reboot.
Click here to read more
Comments