Why unused default export error in React Native Expo project? - reactjs

After installing React Navigation and command expo start:
1.) Simulators have blank white screens
2.) Web port has a blank white screen
3.) App.js now has unused default export
Troubleshooting:
1.) Uninstalled all React Navigation dependancies
2.) Reinstalled all React Navigation dependancies
App.js
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import StackNavigator from './Components/StackNavigator';
export default function App() {
return (
<NavigationContainer>
<StackNavigator />
</NavigationContainer>
);
}
StackNavigator.js
import React from 'react';
import 'react-native-gesture-handler';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import { createDrawerNavigator } from '#react-navigation/drawer';
import HomeScreen from './HomeScreen';
import ResumeScreen from './ResumeScreen';
const StackNavigator = () => {
const Stack = createNativeStackNavigator();
const Drawer = createDrawerNavigator();
return (
<Drawer.Navigator>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
/>
<Stack.Screen
name="Resume"
component={ResumeScreen}
/>
</Stack.Navigator>
</Drawer.Navigator>
);
};
export default StackNavigator;
package.json
{
"name": "portfolio",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"#expo/webpack-config": "^0.17.2",
"#react-native-community/masked-view": "^0.1.11",
"#react-navigation/drawer": "^6.5.6",
"#react-navigation/native": "^6.1.1",
"#react-navigation/native-stack": "^6.9.7",
"expo": "~47.0.8",
"expo-device": "~5.0.0",
"expo-status-bar": "~1.4.2",
"react": "18.1.0",
"react-dom": "^18.1.0",
"react-native": "0.70.5",
"react-native-gesture-handler": "~2.8.0",
"react-native-reanimated": "~2.12.0",
"react-native-safe-area-context": "4.4.1",
"react-native-screens": "~3.18.0",
"react-native-svg": "13.4.0",
"react-native-web": "^0.18.10"
},
"devDependencies": {
"#babel/core": "^7.12.9"
},
"private": true
}

You are trying to combine multiple forms of navigation incorrectly. Try just sticking with Drawer Navigation. Below is a simple working example.
App.js
import React from "react";
import { createDrawerNavigator } from "#react-navigation/drawer";
import { NavigationContainer } from "#react-navigation/native";
import HomeScreen from "./screens/HomeScreen";
import ResumeScreen from "./screens/ResumeScreen";
const Drawer = createDrawerNavigator();
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator initialRouteName="Home">
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Resume" component={ResumeScreen} />
</Drawer.Navigator>
</NavigationContainer>
);
}
package.json
{
"name": "example",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.11",
"#react-navigation/drawer": "^6.1.8",
"#react-navigation/native": "^6.0.6",
"#react-navigation/stack": "^6.0.11",
"expo": "~43.0.2",
"expo-status-bar": "~1.1.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-native": "0.64.3",
"react-native-gesture-handler": "^1.10.3",
"react-native-reanimated": "^2.2.4",
"react-native-safe-area-context": "^3.3.2",
"react-native-screens": "~3.8.0",
"react-native-web": "0.17.1",
"react-navigation": "^4.4.4",
"react-navigation-drawer": "^2.7.1"
},
"devDependencies": {
"#babel/core": "^7.12.9"
},
"private": true
}
screens/HomeScreen.js
import { StyleSheet, Text, View } from "react-native";
import React from "react";
const HomeScreen = () => {
return (
<View>
<Text>HomeScreen</Text>
</View>
);
};
export default HomeScreen;
const styles = StyleSheet.create({});
screens/ResumeScreen.js
import { StyleSheet, Text, View } from "react-native";
import React from "react";
const ResumeScreen = () => {
return (
<View>
<Text>ResumeScreen</Text>
</View>
);
};
export default ResumeScreen;
const styles = StyleSheet.create({});

Related

How to fix Typescript - Next.js build error?

When i build my small Typescript - Nextjs project,
occur type error like this
./components/Layout.tsx:3:55
Type error: Cannot find namespace 'React'.
1 | import NavBar from "#components/NavBar";
2 |
> 3 | export default function Layout({children} :{children :React.ReactNode}){
| ^
4 | return (
5 | <>
6 | <NavBar/>
info - Checking validity of types .%
Layout.tsx is only
import NavBar from "#components/NavBar";
export default function Layout({children} :{children :React.ReactNode}){
return (
<>
<NavBar/>
<div>
{children}
</div>
</>
)
}
and _app.tsx is
import '#/styles/globals.css'
import type { AppProps } from 'next/app'
import Layout from '#components/Layout'
import { Provider } from 'react-redux'
import store from '#/store'
function MyApp({ Component, pageProps }: AppProps) {
return (
<Provider store={store}>
<Layout>
<Component {...pageProps} />
</Layout>
</Provider>
)
}
export default MyApp
and package.json is
{
"name": "bi-front",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "NODE_ENV=development next dev",
"build": "NODE_ENV=development next build",
"start": "NODE_ENV=production next start",
"lint": "NODE_ENV=development next lint"
},
"dependencies": {
"#reduxjs/toolkit": "^1.8.3",
"#types/react-datepicker": "^4.4.2",
"#types/react-redux": "^7.1.24",
"apexcharts": "^3.35.3",
"autoprefixer": "^10.4.7",
"axios": "^0.27.2",
"dayjs": "^1.11.3",
"dotenv": "^16.0.1",
"mongoose": "^6.4.0",
"next": "12.1.6",
"path": "^0.12.7",
"postcss": "^8.4.14",
"react": "^18.2.0",
"react-apexcharts": "^1.4.0",
"react-csv-downloader": "^2.8.0",
"react-datepicker": "^4.8.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.2",
"react-select": "^5.3.2",
"redux": "^4.2.0",
"tailwindcss": "^3.1.3",
"typesafe-actions": "^5.1.0"
},
"devDependencies": {
"#types/node": "18.0.0",
"#types/react": "^18.0.15",
"#types/react-dom": "18.0.5",
"eslint": "8.18.0",
"eslint-config-next": "12.1.6",
"typescript": "4.7.4"
}
}
The reason why i use not only _app.tsx but Layout.tsx is more simply Managing Components.
I think this is simple error,
but this is my first Typescript - Nextjs project,
so i don't know how to fix it.
Please understand that i'm not good at english,
PLEASE HELP ME :(
You have to import React on top of your file, or Import the exact type and use it, e.g.
import { ReactNode } from 'react';
export default function Layout({children}: {children: ReactNode}) {
/* OR */
import React from 'react';
export default function Layout({children}: {children: React.ReactNode}) {

"Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node." on component rerender

I have found an issue that makes me unable to use react-router-dom links that's stated in the title.
Minimal reproduction model:
// index.js
import React from "react";
import client from "react-dom/client";
import App from "./App";
const rootElement = document.getElementById("root");
const root = client.createRoot(rootElement);
root.render(
<App></App>
)
// App.js
import React, {Component} from "react";
import {BrowserRouter as Router} from "react-router-dom";
import Navbar from "./Navbar";
export default class App extends Component {
constructor(props) {
super(props);
this.state = { };
}
componentDidMount() {
setTimeout(() => {
this.setState({authorised: true});
}, 5000);
}
render() {
return (
<Router>
<Navbar authorised={this.state.authorised}/>
</Router>
);
}
}
// Navbar.js
import React from 'react'
import {Link} from "react-router-dom";
export default function Navbar(props) {
if(!props.authorised) return (
<>
<Link to="/">
Home
</Link>
<Link to="/login">
Login
</Link>
<Link to="/register">
Register
</Link>
</>
)
return(
<>
<Link to="/">
Home
</Link>
<Link to="/dashboard">
Dashboard
</Link>
<Link to="/search">
Search
</Link>
</>
)
}
Issue is this, timeout triggers changing a state, state causes the component to be rerendered, Navbar.js causes website to crash with error of
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
While pointing on the Link component.
Am I doing something wrong here? Version of RRD is v6.
edit:
Adding package.json contents on request:
"scripts": {
"start": "node .",
"test": "jest --watchAll",
"build": "webpack --mode production",
"dev_frontend": "webpack-dev-server --mode development --open --hot",
"dev_backend": "nodemon .",
"dev": "concurrently --kill-others \"npm run dev_backend\" \"npm run dev_frontend\""
},
"dependencies": {
"axios": "^0.27.2",
"bcrypt": "^5.0.1",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.0.0",
"express": "^4.18.1",
"express-mysql-session": "^2.1.8",
"express-session": "^1.17.2",
"formik": "^2.2.9",
"jest": "^28.0.3",
"joi": "^17.6.0",
"mysql2": "^2.3.3",
"pug": "^3.0.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-router": "^6.3.0",
"react-router-dom": "^6.3.0",
"supertest": "^6.2.3",
"swagger-jsdoc": "^6.2.1",
"swagger-ui-express": "^4.3.0",
"uuid": "^8.3.2",
"yup": "^0.32.11"
},
"devDependencies": {
"#babel/core": "^7.17.10",
"#babel/preset-env": "^7.17.10",
"#babel/preset-react": "^7.16.7",
"babel-loader": "^8.2.5",
"concurrently": "^7.1.0",
"html-webpack-plugin": "^5.5.0",
"nodemon": "^2.0.16",
"webpack": "^5.72.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.9.0"
}
webpack.config.js
const path = require("path");
const HTMLWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: path.join(__dirname, "views", "index.js"),
output: {
path: path.join(__dirname, "views"),
filename: 'bundle.js'
},
plugins: [
new HTMLWebpackPlugin({
template: path.join(__dirname, "views", "index.html")
})
],
module: {
rules: [
{
test: /.js$/,
exclude: /(node_modules)|(tools)|(routes)|(public)/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env', '#babel/preset-react']
}
}
}
]
},
devServer: {
hot: true
},
target: "web"
}

React create stack navigator

I am using react native and expo. I am getting error message after run expo and it says:
Error: Creating a navigator doesn't take an argument. Maybe you are trying to use React Navigation 4 API with React Navigation 5? See https://reactnavigation.org/docs/upgrading-from-4.x for migration guide.
App.js
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import Home from './components/Home.js'
import Movie from './components/Movie.js'
export default class App extends React.Component {
render() {
return <AppNavigator />
}
}
const AppNavigator = createStackNavigator({
'Home': Home,
'Movie': Movie,
})
Package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.6",
"#react-navigation/native": "^5.4.2",
"#react-navigation/stack": "^5.3.9",
"expo": "~37.0.3",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "^1.7.1",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.2.0",
"react-native-web": "~0.11.7"
},
"devDependencies": {
"babel-preset-expo": "~8.1.0",
"#babel/core": "^7.8.6"
},
"private": true
}
Could you please help to resolve this issue?
I am waiting for your response.
Thanks in advance
You are creating the navigator in the old way, the code for V5 which you are using should be like below.
You will have to import NavigationContainer as well.
import { NavigationContainer } from '#react-navigation/native';
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Movie" component={Movie} />
</Stack.Navigator>
</NavigationContainer>
);
}
More reference can be found here
https://reactnavigation.org/docs/stack-navigator/

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got undefined

I have just started learning how to create apps using React Native. After learning about the Stack Navigator, I attempted to try it out but I am getting this error. Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s%s, 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.
App.js
import React from 'react';
import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationNativeContainer } from '#react-navigation/native';
import {createStackNavigator} from '#react-navigation/stack';
const Stack = createStackNavigator();
export default function App() {
return(
<NavigationNativeContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen}/>
</Stack.Navigator>
</NavigationNativeContainer>
);
}
const HomeScreen=(props)=> {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
Package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.10",
"#react-navigation/core": "^5.7.0",
"#react-navigation/native": "^5.4.0",
"#react-navigation/stack": "^5.3.7",
"expo": "~37.0.3",
"expo-font": "~8.1.0",
"native-base": "^2.13.12",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "^1.8.0",
"react-native-safe-area-context": "^1.0.0",
"react-native-screens": "^2.7.0",
"react-native-web": "~0.11.7"
},
"devDependencies": {
"#babel/core": "^7.8.6",
"babel-preset-expo": "~8.1.0"
},
"private": true
}
change
import { NavigationNativeContainer } from '#react-navigation/native';
to
import { NavigationContainer } from '#react-navigation/native';
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen}/>
</Stack.Navigator>
</NavigationContainer>
When react-navigation was in beta, it was NavigationNativeContainer,now it is NavigationContainer
Hope this helps!
I have an alternative solution that worked for me. In Stack.Navigator, I set the headerMode prop to "none".
<HabitStack.Navigator
headerMode="none"
initialRouteName="HabitManagerScreen"
>
<HabitStack.Screen
name="HabitManagerScreen"
component={HabitManagerScreen}
/>
<HabitStack.Screen name="HabitStatsScreen" component={HabitStatsScreen} />
</HabitStack.Navigator>
This error can happen when you use native-base Image component:
import {Image} from 'native-base';
<Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />
Use react-native Image component instead:
import {Image} from 'react-native';
<Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />

Reactstrap NavDropdown component doesn't open

I am still new to react and am trying to use react and reactstrap for an internal project at work. I have spent several hours trying to the get the NavDropdown component to work. I have tried porting the example code from https://reactstrap.github.io/components/dropdowns/ into my project to just test it out and built my own nav taking cues from this example https://codepen.io/eddywashere/pen/KgjQay?editors=0010
However, I cannot get the Dropdown menu to open.
When I inspect the element I can see the NavDropdown and its child elements in the inspector. When I click on the DropDown element I can see that its state.isOpen toggles to true. However- the dropdown menu doesn't actually show, it remains closed.
I have no console.log errors and am really mystified about what I am doing wrong. Would love to learn my mistake on this one so we can use the library.
Here is the header component I am trying to use the NavDropDown in:
import React from 'react';
import { Navbar, Nav, NavDropdown, DropdownToggle, DropdownMenu, DropdownItem} from 'reactstrap';
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdown: false
};
this.dropdownToggle = this.dropdownToggle.bind(this);
}
dropdownToggle() {
this.setState({
dropdown: !this.state.dropdown
});
}
render() {
return (
<header>
<Navbar color="light">
<Nav navbar>
<NavDropdown isOpen={this.state.dropdown} toggle={this.dropdownToggle}>
<DropdownToggle color="primary" nav caret>
Dropdown
</DropdownToggle>
<DropdownMenu>
<DropdownItem>Header</DropdownItem>
<DropdownItem>Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</NavDropdown>
</Nav>
</Navbar>
</header>
);
}
}
export default Header
Here is App.js file I import the header into:
import React from 'react';
import Header from './shared/Header';
class App extends React.Component {
render() {
return (
<div>
<Header/>
</div>
);
}
}
export default App;`
Here is my index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom'
import App from './comp/App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
import './Index.css';
ReactDOM.render(
<BrowserRouter><App /></BrowserRouter>,
document.getElementById('root')
);
registerServiceWorker();
I used create-react-app to start the project and here is my package.json
{
"name": "estimator",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.0.0-alpha.6",
"node-sass-chokidar": "0.0.3",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-addons-css-transition-group": "^15.6.0",
"react-addons-transition-group": "^15.6.0",
"react-burger-menu": "^2.1.4",
"react-dom": "^15.6.1",
"react-icons": "^2.2.5",
"react-loading": "^0.1.4",
"react-modal": "^2.2.4",
"react-router-dom": "^4.1.2",
"react-scripts": "1.0.11",
"reactstrap": "^4.8.0",
"redux-thunk": "^2.2.0"
},
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"npm-run-all": "^4.0.2"
}
}
I encountered this when I moved from react-bootstrap (using bootstrap 3.3.7) to reactstrap.
Even though my package.json indicated "bootstrap": "^4.0.0-alpha.6" my configuration was still pulling 3.3.7.
I deleted my node_modules/bootstrap folder, did an npm install and it started working.
Maybe something to check?

Resources