Here is a short practice to help you get started with Preact:
- Create a new project using npm:
csharpCopy codenpm init
- Install Preact:
Copy codenpm install preact
- Create a new file
index.js
and import Preact:
javascriptCopy codeimport { h, render } from "preact";
- Create a simple Preact component that displays a message:
javascriptCopy codefunction Message(props) {
return <p>{props.text}</p>;
}
- Render the component to the
body
of the document:
javascriptCopy coderender(<Message text="Hello, Preact!" />, document.body);
- Run your code:
Copy codenode index.js
You should now see the message “Hello, Preact!” displayed in the browser.
- Experiment with changing the props passed to the component, and try adding additional components to the tree.
By following these steps, you’ll be on your way to building more complex Preact components. Happy coding!