Why warning when passing withTheme wrapped component to Route? - react-router-v4

I get the following warning using "react-router": "4.3.1" and "styled-components": "4.1.3"
index.js:2178 Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`.
in Route (created by Routes)
in Routes (created by Route)
in Route (created by withRouter(Routes))
in withRouter(Routes) (created by App)
in div (created by App)
in App (created by Route)
in Route (created by withRouter(App))
in withRouter(App) (created by StartPage)
in div (created by ScrollToTop)
in ScrollToTop (created by Route)
in Route (created by withRouter(ScrollToTop))
in withRouter(ScrollToTop) (created by StartPage)
in ThemeProvider (created by StartPage)
in Router (created by ConnectedRouter)
in ConnectedRouter (created by StartPage)
in Provider (created by StartPage)
in StartPage
This is my Routes class (with irrelevant parts removed):
interface RoutesProps extends RouteComponentProps<{}> {
}
class Routes extends React.Component<RoutesProps, {}> {
public render() {
return (
<Switch>
<Route path="/otii/standard" component={OtiiStandard} />
</Switch>
);
}
}
export default withRouter(Routes);
This the OtiiStandard class (again with most irrelevant parts removed):
import * as React from 'react';
import styled, { withTheme } from 'styled-components';
interface OtiiStandardProps {
// tslint:disable-next-line:no-any
theme: any;
}
const ImageContainer = styled.div`
max-width: 150px;
margin-bottom: 15px;
`;
const Center = styled.div`
text-align: center;
`;
const SectionContainer2 = styled(SectionContainer)`
margin-top: 38px;
`;
// tslint:disable:max-line-length
const OtiiStandard = (props: OtiiStandardProps) => {
return (
<>
<Center>
....
</Center>
</>
);
};
export default withTheme(OtiiStandard);
I can get the warning to go away in two different ways:
A)
Change the route to:
<Route path="/otii/standard" component={() => <OtiiStandard/>} />
B)
Do not wrap OtiiStandard with withTheme
export default OtiiStandard;
Questions
Why does this warning appear? I have many other components that I have written the same way, that are also wrapped by withTheme that do not cause warnings?
Am I fine changing the route to <Route path="/otii/standard" component={() => <OtiiStandard/>} /> or does my code seem to have any problems?

Related

TypeError: Cannot read property 'isFileUploadSupported' of null

I am trying to render a HTML snippet in my React-Native app using react-native-webview
, I also tried react-native-render-html, but in this CSS is not work that much.
This is my Error Stack
ERROR TypeError: Cannot read property 'isFileUploadSupported' of null, js engine: hermes
at App (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:93600:36)
at RCTView
at View
at RCTView
at View
at AppContainer (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:85300:36)
at DBC(RootComponent) (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:89907:28)
ERROR Error: Requiring module "node_modules/react-native-webview/index.js", which threw an exception: TypeError: Cannot read property 'isFileUploadSupported' of null, js engine: hermes
at App (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:93600:36)
at RCTView
at View
at RCTView
at View
at AppContainer (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:85300:36)
at DBC(RootComponent) (http://localhost:8081/index.bundle?platform=android&dev=true&minify=false&app=com.dbc&modulesOnly=false&runModule=true:89907:28)
ERROR TypeError: Cannot read property 'WebView' of undefined
This error is located at:
in App
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in DBC(RootComponent), js engine: hermes
And This is my code
import {SafeAreaView, ScrollView, useWindowDimensions} from 'react-native';
import {WebView} from 'react-native-webview';
import {getTemplate} from './template';
export default class App extends Component {
render() {
const template = getTemplate();
const source = {
html: template,
};
return (
<SafeAreaView>
<ScrollView>
<ScrollView horizontal={true}>
<WebView
ref={r => (this.webref = r)}
originWhitelist={['*']}
source={source}
/>
</ScrollView>
</ScrollView>
</SafeAreaView>
);
}
}

Expo simple Webview, Error: Element type is invalid

I am new to react native and trying to implement this simple webview from Expo docs:
https://docs.expo.dev/versions/latest/sdk/webview/
This is all I am doing and there is not much more to it.
import * as React from 'react';
import { WebView } from 'react-native-webview';
import { StyleSheet } from 'react-native';
import Constants from 'expo-constants';
export default function App() {
return (
<WebView
style={styles.container}
source={{ uri: 'https://google.com' }}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
});
my Package.json looks like this:
"dependencies": {
"expo": "~45.0.0",
"expo-constants": "~13.1.1",
"expo-status-bar": "~1.3.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-native": "0.68.2",
"react-native-webview": "11.18.1"
},
But when I run expo start, I get this error on ios simulator:
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`.
This error is located at:
in App (created by ExpoRoot)
in ExpoRoot
in RCTView (created by View)
in View (created by AppContainer)
in DevAppContainer (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in main(RootComponent)
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`.
This error is located at:
in App (created by ExpoRoot)
in ExpoRoot
in RCTView (created by View)
in View (created by AppContainer)
in DevAppContainer (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in main(RootComponent)

Shopify ResourcePicker not displayed

The Polaris ResourcePicker component is not showing at all in my embedded app and I don't know why. Here is my code:
import React from "react";
import ReactDom from "react-dom";
import * as PropTypes from 'prop-types';
const session = require('express-session');
import {AppProvider, Page } from '#shopify/polaris';
import {ResourcePicker} from '#shopify/app-bridge/actions';
class ExchangeApp extends React.Component {
// This line is very important! It tells React to attach the `easdk`
// object to `this.context` within your component.
static contextTypes = {
easdk: PropTypes.object,
};
state = {
resourcePickerOpen: true,
};
render() {
return <ResourcePicker
resourceType="Product"
open={this.state.resourcePickerOpen}
onSelection={({selection}) => {
console.log('Selected products: ', selection);
this.setState({resourcePickerOpen: false});
}}
onCancel={() => this.setState({resourcePickerOpen: false})}
/>;
}
}
ReactDom.render(
<AppProvider apiKey="532cc1c7fa852e9bbf61c71bcbaa5a74">
<ExchangeApp />
</AppProvider>,
document.querySelector('#root'),
);
In the browser's console I'm getting the following error:
main.js:3584 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `ExchangeApp`.
at invariant (main.js:3584)
at createFiberFromTypeAndProps (main.js:13629)
at createFiberFromElement (main.js:13650)
at reconcileSingleElement (main.js:17327)
at reconcileChildFibers (main.js:17384)
at reconcileChildren (main.js:17753)
at finishClassComponent (main.js:18080)
at updateClassComponent (main.js:18018)
at beginWork (main.js:18864)
at performUnitOfWork (main.js:21679)
Many thanks in advance for any help!
Import ResourcePicker from #shopify/polaris not from easdk.
Also your render return needs to be wrapped in parentheses
return (<ResourcePicker.../>);

Why do I need to export the component to pass it to the registerComponent method?

This code works fine:
import React, { Component } from 'react';
import { Text, AppRegistry } from 'react-native';
export default class App extends Component {
render() {
return (
<Text>Hello World!</Text>
);
}
}
AppRegistry.registerComponent('SampleApp', () => App);
But I get the error when I do not export the App component:
import React, { Component } from 'react';
import { Text, AppRegistry } from 'react-native';
class App extends Component {
render() {
return (
<Text>Hello World!</Text>
);
}
}
AppRegistry.registerComponent('SampleApp', () => App);
Why do I need to export the App component to pass it to the registerComponent method? Is this because AppRegistry.registerComponent is not inside the App class component? Do I need to export class components even if there were in the same file?
Here is the error I get in the iOS simulator:
*I use expo.
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. 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 `ExpoRootComponent`.
This error is located at:
in RootErrorBoundary (at registerRootComponent.js:34)
in ExpoRootComponent (at renderApplication.js:33)
in RCTView (at View.js:60)
in View (at AppContainer.js:102)
in RCTView (at View.js:60)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:32)
throwOnInvalidElementType
ReactNativeRenderer-dev.js:4705:2
createFiberFromElement
ReactNativeRenderer-dev.js:4663:16
reconcileSingleElement
ReactNativeRenderer-dev.js:8337:22
reconcileChildFibers
ReactNativeRenderer-dev.js:8421:12
reconcileChildrenAtExpirationTime
ReactNativeRenderer-dev.js:8621:29
finishClassComponent
ReactNativeRenderer-dev.js:8839:4
updateClassComponent
ReactNativeRenderer-dev.js:8761:11
beginWork
ReactNativeRenderer-dev.js:9580:15
performUnitOfWork
ReactNativeRenderer-dev.js:12924:15
Something needs to be exported in order for it to be imported later on by some other script.
According to MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
The export statement is used when creating JavaScript modules to export functions, objects, or primitive values from the module so they can be used by other programs with the import statement.
My guess is the registerComponent method expects a module as one of the parameters.

React-Native: each child in array should have unique key even when I include the key

I just started React-Native and I came across a problem while dealing with the map of array.As it needs a unique key for all elements.
I have specified the key in the render method but I am still getting the warning.
Here is the code
import React, { Component } from 'react';
import {Text , ScrollView} from 'react-native';
import axios from 'axios';
import AlbumDetail from './AlbumDetail'
export default class AlbumList extends Component {
state = {albums: ['Fetching Data...']};
componentWillMount() {
console.log('fetching data');
// Make a request
axios.get('http://rallycoding.herokuapp.com/api/music_albums')
.then(response => this.setState({albums: response.data}));
}
renderAlbums() {
return this.state.albums.map(album =>
<AlbumDetail
key = {album.title}
album = {album}
/>
);
}
render() {
return(
<ScrollView>
{this.renderAlbums()}
</ScrollView>
);
}
}
And here is the warning I receive even after including the key
ExceptionsManager.js:73 Warning: Each child in an array or iterator should have a unique "key" prop.
Check the render method of `AlbumList`. See https://fb. me/react-warning-keys for more information.
in AlbumDetail (at AlbumList.js:16)
in AlbumList (at index.js:21)
in RCTView (at View.js:113)
in View (at index.js:19)
in FirstApp (at renderApplication.js:35)
in RCTView (at View.js:113)
in View (at AppContainer.js:102)
in RCTView (at View.js:113)
in View (at AppContainer.js:122)
in AppContainer (at renderApplication.js:34)
Look at your error. Its saying the error is in the render call of AlbumList. The code you specified is fine.