"Property does not exist on type" using styled-components in .tsx file - reactjs

I'm attempting to incorporate Styled-Components into a React project using TypeScript. Constructing the component immediately highlights div with the error:
Property 'div' does not exist on type 'typeof import("/Users/admin/node_modules/style-components/dist/index")'.ts(2339)
This is my code:
// App.tsx
import React from 'react'
import './App.css'
import styled from 'style-components'
const StyledDiv = styled.div`
display: flex;
`
const App: React.FC = () => {
return (
<div className="App">
<StyledDiv>
<h1>Testing</h1>
</StyledDiv>
</div>
)
}
Strangely, if I create that styled component in its own .ts file, export it, and import it into .App.tsx it will work just fine. Are styled components only meant to be imported and not used directly in a .tsx file?
My package.json file for informational purposes.
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"styled-components": "^5.3.6"
},
"devDependencies": {
"#types/react": "^18.0.24",
"#types/react-dom": "^18.0.8",
"#types/styled-components": "^5.1.26",
"#vitejs/plugin-react": "^2.2.0",
"typescript": "^4.6.4",
"vite": "^3.2.3"
}
}

I did more testing and created a new component file and immediately I was met with a type error. I started over with a new project folder and used Yarn instead of NPM (which is what I was previously used to). The problem hasn't shown up again.

Related

React - Google SpreadSheet failing to import

Trying to import { GoogleSpreadsheet } from 'google-spreadsheet' in my React App but getting this nasty error
Uncaught ReferenceError: process is not defined
at loadProxy (gaxios.ts:66:5)
at node_modules/gaxios/build/src/gaxios.js (gaxios.ts:75:1)
at __require (chunk-RSJERJUL.js?v=0db569af:3:50)
at node_modules/gaxios/build/src/index.js (index.ts:15:1)
at __require (chunk-RSJERJUL.js?v=0db569af:3:50)
at node_modules/gcp-metadata/build/src/index.js (index.ts:8:1)
at __require (chunk-RSJERJUL.js?v=0db569af:3:50)
at node_modules/google-auth-library/build/src/auth/googleauth.js (googleauth.js:19:21)
at __require (chunk-RSJERJUL.js?v=0db569af:3:50)
at node_modules/google-auth-library/build/src/index.js (index.js:17:22)
here below is a screenshot of my current app structure that I have started using Vite (which maybe is why this is not working?)
package.json
{
"name": "tinder-triage",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"google-spreadsheet": "^3.3.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"#types/react": "^18.0.27",
"#types/react-dom": "^18.0.10",
"#vitejs/plugin-react": "^3.1.0",
"vite": "^4.1.0"
}
}
vite.config.js
import { defineConfig } from 'vite'
import react from '#vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
})

How to resolve './components/AppFooter' or its corresponding type declarations. Error on Netlify and railway.app

Hhile deploying I am getting error, it is Vite + React + Typescript app.
9:27:22 AM: $ tsc && vite build
9:27:23 AM: src/App.tsx(2,23): error TS2307: Cannot find module './components/AppFooter' or its corresponding type declarations.
9:27:23 AM: src/App.tsx(3,23): error TS2307: Cannot find module './components/AppHeader' or its corresponding type declarations.
Where I am getting error, appheader and appfooter contains just printing "working".
import "./App.css";
import AppFooter from "./components/AppFooter";
import AppHeader from "./components/AppHeader";
function App() {
return (
<>
<AppHeader></AppHeader>
<AppFooter></AppFooter>
</>
);
}
export default App;
My file structure:
package.json file:
{
"name": "vite---react---typescript",
"private": true,
"version": "0.0.0",
"scripts": {
"start": "npx http-server ./dist",
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"#types/react": "^18.0.0",
"#types/react-dom": "^18.0.0",
"#vitejs/plugin-react": "^1.3.0",
"autoprefixer": "^10.4.13",
"postcss": "^8.4.20",
"tailwindcss": "^3.2.4",
"typescript": "^4.6.3",
"vite": "^2.9.9"
}
}
I tried changing the casing and changing the file name, it is working perfectly fine on my local but not when I deploy.
It looks like your components are in ./components/main, so you will need to update the paths on the imports, e.g.
import AppFooter from "./components/main/AppFooter";

Cannot read properties of undefined (reading 'displayName') for Cypress component test

I have a working nextjs app and am wanting to add Cypress component tests for my react components.
e2e cypress tests are working without issue, but when trying to implement component tests I'm receiving the error, "Cannot read properties of undefined (reading 'displayName')" when trying to mount a component.
I followed the documentation at https://docs.cypress.io/guides/component-testing/component-framework-configuration#Next-jsn and also accepted the configuration that was generated by the cypress tool when I ran yarn cypress.
My code with component testing issue is at https://gitlab.com/kennyrbr/activ/-/tree/cypress-testing/web
Here is where I'm attempting to mount .
import React from 'react';
import { mount } from '#cypress/react';
import { NavBar } from '../../components/NavBar';
describe('NavBar.cy.ts', () => {
it('playground', () => {
cy.mount(`<NavBar />`);
});
});
My package.json is
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"gen": "graphql-codegen --config codegen.yml",
"cypress": "cypress open"
},
"dependencies": {
"#graphql-codegen/typescript-urql": "^3.6.1",
"#urql/exchange-graphcache": "^4.4.3",
"daisyui": "^2.22.0",
"formik": "^2.2.9",
"graphql": "^16.5.0",
"next": "latest",
"next-urql": "^3.3.3",
"react": "18.1.0",
"react-dom": "18.1.0",
"react-is": "^18.2.0",
"urql": "^2.2.2",
"yup": "^0.32.11"
},
"devDependencies": {
"#cypress/react": "^6.1.1",
"#cypress/webpack-dev-server": "^2.2.0",
"#graphql-codegen/cli": "^2.8.0",
"#graphql-codegen/typescript": "^2.7.1",
"#graphql-codegen/typescript-operations": "^2.5.1",
"#testing-library/cypress": "^8.0.3",
"#types/node": "17.0.35",
"#types/react": "18.0.9",
"#types/react-dom": "18.0.5",
"autoprefixer": "^10.4.7",
"cypress": "^10.6.0",
"html-webpack-plugin": "^5.5.0",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.2",
"typescript": "4.7.2",
"webpack": "^5.74.0",
"webpack-dev-server": "^4.10.0"
}
}
Here's the error which displays after running yarn cypress and choosing component testing for the 'NavBar.cy.ts' test.
N.B., I did add a few dev dependencies because I was receiving a webpack warning early on.
Any help is greatly appreciated.
edit:
I had added an incorrect displayName assignment to some of my components in an attempt to get cypress component testing working - and had forgotten to remove in a previous commit. This wasn't even the issue as displayName couldn't be read from undefined, so I think the issue is that the component/s aren't available to cypress. In any case, it was wrong and I've removed.
The basic problem is the parameter you are passing into cy.mount() isn't being compiled to JSX.
Two things should fix it
take the quotes off the JSX, so it should be cy.mount(<NavBar />);
change the extension of the spec file to NavBar.cy.tsx
It's not really a working nextjs app, you are getting the exact same message from a different component SelectActivType.tsx when you you run the app (outside of the Cypress test).
In terms of the NavBar, the code should be adjusted from this
export const NavBar: React.FC<NavBarProps> = ({}) => {
NavBar.displayName = 'ActivTypes';
const router = useRouter();
return (
...
);
}
to this
export const NavBar: React.FC<NavBarProps> = ({}) => {
const router = useRouter();
return (
...
);
}
// move the offending line outside of the function
NavBar.displayName = 'ActivTypes';
You cannot add a property to a function inside the function definition.
For reference see how to set displayName in a functional component [React].

when trying to use styled-components in next js, node module in react js have error

I want to use styled-component in nextjs react .
This is how I use styled-component:
import styled from 'styled-components';
export const MainTitleContainer = styled.div`
text-align:right;
margin:3rem 0;
padding:2rem;
`;
export const MainTitle = styled.h1`
font-size:3.8rem;
font-family:Bnazanin;
`;
import {MainTitleContainer,MainTitle} from '../styles/Home.styles';
As soon as I import styled-components (import styled from 'styled-components';) into my nextjs app I get the following error:
../../node_modules/styled-components/dist/styled-components.browser.esm.js:1:73
Module not found: Can't resolve 'react'
null
error image
Here is my package.json:
{
"name": "next-horse-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"next": "11.0.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"styled-component": "^2.8.0"
},
"devDependencies": {
"babel-plugin-styled-components": "^1.13.2",
"eslint": "7.31.0",
"eslint-config-next": "11.0.1"
}
}
How can I fix this error?
It looks like the "s" is missing in package.json for "styled-components", try running removing node_modules, and running npm i styled-components

Type error Module '"react-leaflet"' has no exported member 'useEventHandlers'

I have nextjs app + typescript + react-leaflet
when I start my development server npm run dev it's fine there is no error,
but when I start build with npm run build I have an error, it says.
Type error: Module '"react-leaflet"' has no exported member 'useEventHandlers'.
1 | import { useMemo, useCallback, useState } from 'react'
> 2 | import { useMap, useMapEvent, useEventHandlers, MapContainer } from 'react-leaflet'
| ^
3 |
4 | // Classes used by Leaflet to position controls
5 | const POSITION_CLASSES = {
info - Checking validity of types .
Here's my package.json file.
{
"name": "spot-nearby",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"#types/leaflet": "^1.7.0",
"#types/react-leaflet": "^2.8.1",
"axios": "^0.21.1",
"csv-parser": "^3.0.0",
"leaflet": "^1.7.1",
"leaflet-geosearch": "^3.3.2",
"next": "10.2.3",
"papaparse": "^5.3.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-leaflet": "^3.2.0",
"react-leaflet-search": "^2.0.1",
"react-loader-spinner": "^4.0.0",
"react-papaparse": "^3.14.0",
"react-search-field": "^1.2.1"
},
"devDependencies": {
"#types/react": "17.0.8",
"typescript": "4.3.2"
}
}
I already install #types/react-leaflet, but why did this happened?
react-leaflet in fact does not export useEventHandlers method. It's a method from #react-leaflet/core package. It's a dependency of react-leaflet package and must be installed along with it. And should be imported from there:
import { useMap, useMapEvent, MapContainer } from 'react-leaflet'
import { useEventHandlers } from '#react-leaflet/core'

Resources