PascalCase React Snippet in VS Code - reactjs

I am using the "ES7+ React/Redux/React-Native snippets" extension VS Code. I like to use rfc to generate a React functional component based on the name of the file. The issue is I want to use Next.js / next router to handle my pages.
I want the route the browser hits to be /blah/this-is-the-page so the file needs to be /pages/blah/this-is-the-page.tsx. If I run the rfc snippet from this file it creates a function called this-is-the-page ie export default function this-is-the-page() {. The preferred behavior, when there are dashes in the file name, would be to use PascalCase like this: ThisIsThePage ie export default function ThisIsThePage() {.
Is there a way to customize or indicate to this snippet extension to use PascalCase in this situation?
https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets

Related

Is it possible to use hooks in _document.js (NextJS)?

I'm trying to append a class to my body tag depending on the locale. So if someone visits from Spain, I will append a class which changes the Tailwind theming to match the branding of that country.
My body tag is located in a custom _document.js file. When I try to import { useRouter } from 'next/router', I get the 'breaking rules of hooks' error in React. I need this hook to detect the locale and therefore append the class if necessary.
Is it possible to use hooks in _document.js? If not, why not?

How can I specify that an exported function should not be treated as a JSX component? (React)

I was trying to debug a 'expected 0 arguments but got 1' error and I found out that my function is considered a JSX element, which probably causes the issue. How can I specify it is a regular function?
Above is the function I am trying to export from a .tsx file and then use it in another file like so:
pay(10).
How can I do that? There is a functional component in the same .tsx file by the way.
I had an import problem so it considered the default export only.

Word Document Cannot find module or it's corresponding type declarations

I am getting this error trying to import this file from src/assets/documents folder.
I am working on a React Project with Typescript. I am trying to import the file and feed it's value in an anchor tag so it can be downloaded.
When I'm importing images from src/assets/images, this problem is not existing.
Can anyone help?
I had the same problem.
Since you are using TypeScript you will need to define the type of the data you are importing.
The solution is fairly simple you need to create a proxy file which will map the types.
Usually the of the file ends with .d.ts
In Vue for example the convention is shims-vue.d.ts
Click [here][1] to see an example
The content of the fie should be:
declare module '*.doc' {
import nameOfDoc from src/assets/documents (should be your path to the doc)
export default nameOfDoc
}

Import a file as a string (or source asset) in Gatsby / React

I want to import .ts, .tsx, .js, and .jsx files into a react component and render them within a PrismJS highlighting block. For example, let's say I have a TypeScript file with functionA in it that I want to highlight in my actual website:
functionA.ts:
export function functionA() {
console.log("I am function A!");
}
I want to include this in a different component. The problem is, when I import it, I am obviously importing the webpack module version of it. My weak attempt at trying to get my function render in a react component looks like this:
MyComponent.tsx:
import * as React from "react"
import { functionA } from "./functionA"
export function MyComponent() {
return (
<>
<h1>Here is your code block:</h1>
<pre>
<code>
{functionA.toString()}
</code>
</pre>
</>
)
}
and what will actually render on the page where the code block is will look something like this:
Here is your code block:
WEBPACK__IMPORT.functionA() {
console.log("I am function A!")
}
I can't exactly remember what the .toString() function output looked like, but the point is it is NOT just the contents of the file how it appears in a code edit for example - it has been modulized by WebPack.
So, in a Gatsby project, how can i get these various code snippets to be imported directly as a string, purely as they are written, without WebPack enacting its import stuff on it? Is there a plugin or some way to tell Webpack to use the imported file as its asset/source module type? I know for MD or MDX files there is the gatsby-remark-embed-snippet, but I am building a component based HTML page and can't use MD or MDX files!
It's very late, and perhaps I just can't see the forest from the trees, I know there must be a way to do this...
You need to require the file using webpack's raw-loader, i.e:
const functionA = require("!!raw-loader!./functionA");
This works for create-react-app, as in the solution discussed here, and this works for Gatsby as well!
After using require on such a file, the file contents can be rendered in the component as:
<pre>{functionA.default.toString()}</pre>
It's then up to you to add syntax highlighting using a tool like prism or similar.
Note this solution will only work as long as Gatsby V3 continues to use WebPack v4, as raw-loader is deprecated in WebPack v5 and will be phased out for asset/source type modules.

Reactjs overide markdown types with react-markdown

I am using contentful to get markdown to a react component that uses react-markdown to parse the markdown
import ReactMarkdown from 'react-markdown';
<Markdown source={text} />
Would I like to do is to override the Renderer so instead of it rendering ## as an h2 render i can pass a custom component to override the default h2 type to my own h2 component. How can i do that and is there and examples?
One of the options to <ReactMarkdown> is renderers.
One of the common renderers handles headings. If you look at the default rendering you'll see this:
heading: function Heading(props) {
return createElement('h' + props.level, getCoreProps(props), props.children);
},
So pass in your own heading handler. Check the level inside, roughly:
function CustomHeading(props) {
if (props.level !== 2) {
return createElement(`h${props.level}`, getCoreProps(props), props.children);
}
return <MyCustomElement {...props} />
}
If you don't have access to the code that commonmark-react-renderer gives you in the context of your function (which you probably won't) then you'd also need to duplicate what createElement gives you (but it's simple).
Unrelated: I've never used <ReactMarkdown> (but will), but this took me about five minutes of research. I'm including my path to encourage others to dig into their own questions and hopefully give some insight into how such things can be researched.
The react-markdown home page
Scanned through the "Options" section to see if custom rendering was trivially supported
Found the renderers option, which sounded promising
Clicked the link provided in that option's docs
Saw that heading was one of those (which made sense; I'd expect a renderer for every major formatting that Markdown supports)
Opened up the src directory to see if the implementation was easy to find
There was only one file, so I opened it
Searched the page for "heading" and found it
Cut and pasted that code here
The ability to read docs and follow trails is really important.

Resources