Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invariant Violation: Invariant Violation: Text strings must be rendered within a <Text> component. #23607

Closed
m-ret opened this issue Feb 23, 2019 · 3 comments
Labels
Bug Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.

Comments

@m-ret
Copy link

m-ret commented Feb 23, 2019

I am having this weird error on this component:

Invariant Violation: Invariant Violation: Text strings must be rendered within a component.

This is the component:

import { View, Text } from 'react-native';
import React from 'react';
import { filter, size } from 'lodash';
import PropTypes from 'prop-types';
import { compose } from 'redux';
import { connect } from 'react-redux';
import PassengersInfo from './PassengerInfo';
import OptionsModal from '../PopupsModals/OptionsAlertPassenger';
import AllPassengersOptionsModal from '../PopupsModals/AllPassengersOptionsModal';

import { popupsModalsAction } from '../PopupsModals/actions/popupsModals';

const PassengerCardBasedOnRoute = ({
  navigationStore,
  passengersData,
  popupsModalsActionHandler,
  searchParam,
}) => {
  const filterBy = (key, val) => filter(passengersData, [key, val]);

  const search = searchParam.toLowerCase();

  const filteredData = filterByParam =>
    filterByParam.filter(obj =>
      Object.keys(obj).some(key =>
        String(obj[key])
          .toLowerCase()
          .includes(search),
      ),
    );

  const componentToRenderBasedOnParams = info => (
    <PassengersInfo
      key={info.id}
      id={info.id}
      cardinalpoint={info.cardinalpoint}
      name={info.name}
      address={info.address}
      datetime={info.timestamp}
      searchParam={searchParam}
      callModal={popupsModalsActionHandler}
    />
  );

  const showFeedbackIfNoLength = (filterByParam, value, indexRoute) => {
    if (size(filteredData(filterBy(filterByParam, value)))) {
      filteredData(filterBy(filterByParam, value)).map(info =>
        componentToRenderBasedOnParams(info),
      );
    } else {
      return indexRoute && <Text>No records found</Text>;
    }

    return filteredData(filterBy(filterByParam, value)).map(info =>
      componentToRenderBasedOnParams(info),
    );
  };

  return (
    <>
      <OptionsModal>{<AllPassengersOptionsModal />}</OptionsModal>
      <View>
        {!navigationStore.index &&
          passengersData &&
          showFeedbackIfNoLength('pickup', 1, !navigationStore.index)}

        {passengersData && navigationStore.index &&
           showFeedbackIfNoLength(
              'dropofftimestamp',
              null,
              navigationStore.index,
            )}
      </View>
    </>
  );
};

PassengerCardBasedOnRoute.propTypes = {
  navigationStore: PropTypes.shape({}).isRequired,
  passengersData: PropTypes.oneOfType([PropTypes.array]).isRequired,
  popupsModalsActionHandler: PropTypes.func.isRequired,
  searchParam: PropTypes.oneOfType([PropTypes.string]).isRequired,
};

export default compose(
  connect(
    store => ({
      navigationStore: store.homeScreen.navigation,
      passengersData: store.homeScreen.passengersData,
      searchParam: store.homeScreen.searchParam,
    }),
    dispatch => ({
      popupsModalsActionHandler: data => {
        dispatch(popupsModalsAction(data));
      },
    }),
  ),
)(PassengerCardBasedOnRoute);

The problem is right here:

        {passengersData && navigationStore.index &&
           showFeedbackIfNoLength(
              'dropofftimestamp',
              null,
              navigationStore.index,
            )}

I can get rid of that error by doing this:

      {passengersData && navigationStore.index
          ? showFeedbackIfNoLength(
              'dropofftimestamp',
              null,
              navigationStore.index,
            )
          : null}

But, why?

I checked my file and there is no whitespace or something similar.

@react-native-bot
Copy link
Collaborator

Thanks for submitting your issue. Can you take another look at your description and make sure the issue template has been filled in its entirety?

👉 Click here if you want to take another look at the Bug Report issue template.

@react-native-bot react-native-bot added Ran Commands One of our bots successfully processed a command. Resolution: Needs More Information labels Feb 23, 2019
@ericlewis
Copy link
Contributor

ericlewis commented Feb 23, 2019

I have run in to this problem before with iOS, essentially what happens is when you do things like:

passengersData && navigationStore.index && <Component />

if the first condition fails, it returns false, which isn't valid to render- so you must return null to explicitly not render something. It seems to be something that happens when running in release mode for me, but I have also seen it when debugging.

@hramos
Copy link
Contributor

hramos commented Feb 23, 2019

Duplicate of #20764

@hramos hramos closed this as completed Feb 23, 2019
@hramos hramos marked this as a duplicate of #20764 Feb 23, 2019
@facebook facebook locked as resolved and limited conversation to collaborators Feb 23, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Feb 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Ran Commands One of our bots successfully processed a command. Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants