Categories
React Answers

How to fix ‘cannot be used as a JSX component’ error with TypeScript and React?

To fix the error ‘cannot be used as a JSX component’ error with React TypeScript, we need to make sure our component returns a single root element.

For instance, we write

function Items(): JSX.Element {
  return (
    <>
      {items.map((item) => (
        <li>{item.name}</li>
      ))}
    </>
  );
}

to put our array of items in the Items component in a fragment so that the component can be compiled.

We call items.map to render an array of li elements so we need to wrap them with a fragment.

Categories
React Answers

How to add form validation with React MUI?

To add form validation with React MUI, we set the error prop to true when there’s an error.

For instance, we write

import * as React from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";

export default function ValidationTextFields() {
  return (
    <Box
      component="form"
      sx={{
        "& .MuiTextField-root": { m: 1, width: "25ch" },
      }}
      noValidate
      autoComplete="off"
    >
      <div>
        <TextField
          error
          id="outlined-error"
          label="Error"
          defaultValue="Hello World"
        />
        <TextField
          error
          id="outlined-error-helper-text"
          label="Error"
          defaultValue="Hello World"
          helperText="Incorrect entry."
        />
      </div>
    </Box>
  );
}

to add the error prop to the TextFields to show a red outline to indicate to the user that the input value is invalid.

Categories
JavaScript Answers

How to convert HTML to PDF with Node.js and JavaScript?

To convert HTML to PDF with Node.js and JavaScript, we use the html-pdf-node package.

To install it, we run

npm i html-pdf-node

Then we write

const htmlToPdf = require("html-pdf-node");

const options = { format: "A4" };
const file = { content: "<h1>Welcome to html-pdf-node</h1>" };
const pdfBuffer = await htmlToPdf.generatePdf(file, options);
console.log("PDF Buffer:-", pdfBuffer);

to convert the file.content string into a buffer with the PDF file with the generatePdf function.

We can also write

const htmlToPdf = require("html-pdf-node");

const options = { format: "A4" };
const file = { url: "https://example.com" };
const pdfBuffer = await htmlToPdf.generatePdf(file, options);
console.log("PDF Buffer:-", pdfBuffer);

to call generatePdf to get the PDF from the file.url URL and convert that to a PDF buffer.

A promise is returned so we use await to get the pdfBuffer.

Categories
React Answers

How to fix Invariant Violation: Objects are not valid as a React child error?

To fix Invariant Violation: Objects are not valid as a React child error, we should make sure we put items in the braces that can be rendered.

For instance, we write

const Comp = () => {
  return (
    <>
      <p>{"foo"}</p>
      <p>{1}</p>
      <p>{null}</p>
    </>
  );
};

to render 'foo', 1 and null in the p elements.

null will render nothing.

And the other values will be displayed on the screen.

Objects can’t be rendered directly on the screen, so we’ve to convert them to strings with JSON.stringify if we want to show them on the screen.

For instance, we write

const Comp = () => {
  return (
    <>
      <p>{JSON.stringify({ foo: "bar" })}</p>
    </>
  );
};

to call JSON.stringify to convert { foo: "bar" } to a JSON string so they can be rendered.

Categories
JavaScript Answers

How to fix referenceerror: exports is not defined in ES module scope with JavaScript?

To fix referenceerror: exports is not defined in ES module scope with JavaScript, we should make sure we’re using export in an ES module instead of module.exports.

For instance, we write

const foo = () => {};

export { foo };

to create an ES module that exports the foo function as a named export.

We can also create a default export with

const foo = () => {};

export default foo;

to export foo as a default export.

module.exports is only used in CommonJS modules to export their members.