React-profiler: element type is invalid - reactjs

based on React Docs and this medium article, I did something as simple as this to try profiler in react
import React, { unstable_Profiler as Profiler } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends React.Component {
logProfile = (id, phase, actualTime, baseTime, startTime, commitTime) => {
console.log(`${id}'s ${phase} phase:`);
console.log(`Actual time: ${actualTime}`);
console.log(`Base time: ${baseTime}`);
console.log(`Start time: ${startTime}`);
console.log(`Commit time: ${commitTime}`);
};
render () {
return (
<div>
<Profiler id="app" onRender={this.logProfile}>
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
</Profiler>
</div>
);
}
}
export default App;
but this is throwing following error
Error: Element type is invalid: expected a string (for built-in
components) or a class/function (for composite components) but got:
undefined. You likely forgot to export your component from the file
it's defined in, or you might have mixed up default and named imports.
Check the render method of App.
Can someone tell me what I am doing wrong here?

I face the same issue and I change the import object from unstable_Profiler to Profiler directly and it works.
// import React, { unstable_Profiler as Profiler } from 'react';
import React, { Profiler } from 'react';
const App = () => (
<div>
<Profiler>
<OtherComponents />
</Profiler>
</div>
)
I check the test scenario of Profiler here at github.

Related

React error with code splitting (Argument type function() is not assignable to parameter type)

I am trying to split my code using React.Lazy() with Route-based code splitting (https://reactjs.org/docs/code-splitting.html)
For example that's the component (Reports.jsx)
import React from 'react';
const Reports = () => {
return <div>Reports</div>;
};
export default Reports;
And I have in my route.js:
const LazyReports = React.lazy(() => import ('../../containers/team/reports/Reports'));
and I'm getting this error, and the bundle is not loaded.
Argument type function(): Promise<{readonly default?: function()}> is not assignable to parameter type () => Promise<{default: ComponentType}>   Type Promise<{readonly default?: function()}> is not assignable to type Promise<{default: ComponentType}>
I figured out it's not related to the routing, but I don't know why it doesn't recognize the component as "ComponentType"
I am not sure about the complete code but below code is working fine with lazy code syntax. I feel above seem ok to me. Hoping the container code is only have above component code.
One thing you can do , delete the build folder and build again with yarn build/npm build.
Below is simple code without any router /redux.
import React from 'react';
import logo from './logo.svg';
import './App.css';
const LazyReports = React.lazy(() => import ('./Reports'))
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
<LazyReports/>
</div>
);
}
export default App;

Can't find module TS2307 in React

import React from "react";
import logo from "./logo.svg";
import text from "./compiler/test/index.txt";
import "./App.css";
const App: React.FC = () => {
// const compiler = new Compiler("../test/index.jack");
// compiler.save();
console.log("text: ", text);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
};
export default App;
I'm using react-app-rewired to override webpack configuration, without ejecting.
This component reads a .txt file and logs its content, but there is an error, Cannot find module './compiler/test/index.txt'. TS2307 in the third line of import.
As my comment seemed to resolve the issue, here as an official answer:
You probably just need to declare the type .txt somewhere in your typescript definitions. For create-react-app, this would be in react-app-env.d.ts. E.g.:
declare module '*.txt' {
const content: string;
export default content;
}

Unable to get past the 'React App' main screen

I'm not able to get a 'react' application beyond Screen with the "atom icon" and "learn React" link.
There is a message saying 'Edit src/App.js and save to reload.'
But there's nothing giving any information or clue about what changes need to be made to the App.js file, what configuration or other settings to check (and what the values should be), etc.
What do I need to do to get a React app to actually work?
//=========================
// BEGIN App.js SourceCode:
// ------------------------
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
// ----------------------
// END App.js SourceCode
//=======================
The changes you need to make to App.js is the code for the initial page of your React App.
Replace your code with this, and change the "Hello":
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<p>Hello</p>
</div>
);
}
export default App;

Jest snapshot testing not generating appropriate snapshot and passing

I'm new at jest testing. Now I'm trying to test my react components using snapshot testing. I've encountered such problem: jest only updates snapshot for AppComponent, but does not for another component, also it passes all both snapshot tests. What's wrong?
Have tried passing names to snapshot toMatchSnapshot(), have tried to change type of TestComponent from function to class extending Component, have tried deleting old snapshots and testing again, all in wain. Also, tried to render TestComponent in AppComponent below AppComponent default html and it caused updating AppComponent snapshot only.
AppComponent
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { TestComponent } from './TestComponent';
export function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
</div>
);
}
export default App;
AppComponent test
import React from 'react';
import ReactDOM from 'react-dom';
import { App, reverseOfReverse, addTwoIntegers } from './App';
import renderer from 'react-test-renderer';
describe('snapshot tests', () => {
test('matches the snapshot', () => {
const tree = renderer.create(<App />).toJSON();
expect(tree).toMatchSnapshot('AppComponentSnapshot');
});
});
AppComponent snapshot
// Jest Snapshot v1
exports[`snapshot tests matches the snapshot: AppComponentSnapshot 1`] = `
<div
className="App"
>
<header
className="App-header"
>
<img
alt="logo"
className="App-logo"
src="logo.svg"
/>
<p>
Edit
<code>
src/App.js
</code>
and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
rel="noopener noreferrer"
target="_blank"
>
Learn React
</a>
</header>
</div>
`;
TestComponent
import logo from './logo.svg';
import React, { Component } from 'react';
export class TestComponent extends Component {
render() {
return (
<div className="TestComponent">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer">
Learn React
</a>
</header>
<div>lola</div>
<div>amanda</div>
</div>
);
}
}
export default TestComponent;
TestComponent test
import React from 'react';
import renderer from 'react-test-renderer';
import { TestComponent } from './TestComponent';
describe('snapshot for second component', () => {
test('matches second snapshot', () => {
const tree = renderer.create(<TestComponent />).toJSON;
expect(tree).toMatchSnapshot('TestComponentSnapshot');
});
});
TestComponent snapshot
// Jest Snapshot v1
exports[`snapshot for second component matches second snapshot: TestComponentSnapshot 1`] = `[Function]`;
I expected that snapshot test would also fill TestComponent snapshot with appropriate snapshot, or the test would fail, but neither happened.
I just faced this problem too. Spent an hour to realize that toJSON is missing a pair of parentheses. It's supposed to be a function call toJSON() instead of just toJSON. How silly

Reactjs - react-reponsive-carousel

Hi I'm a newb to reactjs and I wanted to try pulling some code from other libraries to see how it works. Unfortunately, I'm sort of stuck on trying to use the react-responsive-carousel. I realize this is a really simple question but I have been stuck on this for quite some time now and would appreciate any tips! Thank you so much!
Here is what I'm currently trying:
Index.jsx
import React from 'react';
import {render} from 'react-dom';
import AwesomeComponent from './AwesomeComponent.jsx';
import CarouselComponent from './Carousel.jsx'
class App extends React.Component {
render() {
return (
<div>
<p>Hello React!</p>
<AwesomeComponent />
<CarouselComponent />
</div>
);
}
}
render(<App/>, document.getElementById('container'));
Carousel.jsx:
import React from 'react';
import Carousel from 'react-responsive-carousel';
class CarouselComponent extends React.Component {
render() {
return (
<div>
<Carousel>
<div>
<img src="assets/1.jpeg" />
<p className="legend">Legend 1</p>
</div>
<div>
<img src="assets/2.jpg" />
<p className="legend">Legend 2</p>
</div>
<div>
<img src="assets/3.jpg" />
<p className="legend">Legend 3</p>
</div>
</Carousel>
</div>
);
}
}
export default CarouselComponent;
The error I'm seeing in the console right now is:
Warning: React.createElement: type is invalid -- expected a string (for
built-in components) or a class/function (for composite components) but got:
undefined. You likely forgot to export your component from the file it's
defined in. Check the render method of `CarouselComponent`.
in CarouselComponent (created by App)
in div (created by App)
in App
Try importing react-responsive-carousel with braces. Like:
import { Carousel } from 'react-responsive-carousel';

Resources