Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix '_react["default"].memo is not a function. (In '_react["default"].memo(connectFunction)' error in React native?

I am trying to connect Redux mapStateToProps() and mapActionCreators() to Login Screen through Container and I am using React navigation.

After build my app the following error message appears:

_react["default"].memo is not a function. (In '_react["defaults"].memo(connectFunction)', '_react["defaults"].memo' is undefined.

I searched for a while but what I have gotten is that React.memo() helps us control when our components rerender but I don't use any code related to React.memo().

Login Screen: (screens/LoginScreen/index.js)

import React from 'react';
import {Dimensions, View} from 'react-native';

//Get width of mobile screen
var width = Dimensions.get("window").width;
var height = Dimensions.get("window").height;

export default class LoginScreen extends React.Component {
    constructor(props){
        super(props);
        this.state = {

        }
    }

    render() {
        return (
            <View style={styles.container}>
                <Text>Log In page</Text>
            </View>
        );
    }
}

LoginScreen.defaultProps = {
}

const styles = {
    container: {
        flex: 1
    }
}

Login screen container: (containers/LoginContainer/index.js)

import {connect} from "react-redux";
import LoginScreen from "../../screens/LoginScreen";

const mapStateToProps = (state) =>({

});


const mapActionCreators = {

};
export default connect(mapStateToProps, mapActionCreators)(LoginScreen);

Top level navigation: (navigations/TopLevelSwitchNav.js)

import {createSwitchNavigation, createAppContainer} from 'react-navigation';
import LoginScreen from '../containers/LoginContainer';
import MainTabNav from './MainTabNav';


const TopLevelSwitchNav = createSwitchNavigation({
    Login:  {
        screen: LoginScreen,
        navigationOptions: {
            header: null
        }
    },
    MainTab: {
        screen: MainTabNav,
        navigationOptions: {
            header: null
        }
    }
},
{
    initialRouteName: Login,
    navigationOptions: { header: null }
});

export default createAppContainer(TopLevelSwitchNav);

Dependencies:

"dependencies": {
    "expo": "^32.0.0",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react-navigation": "^3.8.1",
    "react-redux": "^7.0.2",
    "redux": "^4.0.1",
    "redux-logger": "^3.0.6",
    "redux-persist": "^5.10.0",
    "redux-persist-transform-filter": "^0.0.18",
    "redux-thunk": "^2.3.0"
},
like image 291
Ahmed Saeed Avatar asked Apr 15 '19 14:04

Ahmed Saeed


People also ask

Can we use react memo in functional component?

React. memo() was introduced with React 16.6 to avoid unnecessary re-renders in functional components. It is a higher-order component that accepts another component as a prop. It will only render the component if there is any change in the props.

Why memo is used in react?

Using memo will cause React to skip rendering a component if its props have not changed. This can improve performance.

How do you use react memo in react JS?

React.memo is a higher order component. If your component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.

Does react memo work with hooks?

memo() and hooks. Components using hooks can be freely wrapped in React. memo() to achieve memoization. React always re-renders the component if the state changes, even if the component is wrapped in React.


2 Answers

I solved it, it seems that React-Redux package depend on React 16.8.0 and uses React.memo() somehow so I downgraded React-Redux to v6.0.0

like image 140
Ahmed Saeed Avatar answered Sep 21 '22 15:09

Ahmed Saeed


Same as the solutions above but with instructions. Be sure to clear the Expo cache!

Downgrade your react-redux to version 6.0.1

npm install [email protected]

Clear the cache in Expo

expo r -c
like image 41
Mitchell Hudson Avatar answered Sep 17 '22 15:09

Mitchell Hudson