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

Raw text cannot be used outside of a <Text> tag when using style property #269

Open
kimfucious opened this issue Aug 27, 2020 · 7 comments

Comments

@kimfucious
Copy link

kimfucious commented Aug 27, 2020

Hi there,

I am probably doing something wrong, if not a lot of things, but I can't figure out what. I have Googled, and I have read through the issues here, but to no avail.

In brief, after a recent upgrade on a project to "eslint-plugin-react-native": "^3.8.1", I'm getting all kinda warnings.

These seem to be only on elements where I have implemented in-line styles, but there's so many, I'm not 100% sure.

If I delete the style prop, the warning goes away, but then so does the styling.

Here are a couple examples:

<Text style={{ ...theme.typography.subtitle, textAlign: "center" }}>
  To optimize your experience...
</Text>
<Text
  style={{
    ...theme.typography.title,
    textAlign: "center"
  }}
>
  Connect
  {primaryMusicStream === "spotify"
    ? " " + getPrimaryMusicStreamTitleCase(primaryMusicStream)
    : null}
</Text>
<Text style={theme.typography.subtitle}>
  Your Spotify playlist has been cleaned
</Text>

I'm not using StyleSheets, and theme.xxx is referencing an imported style sheet that looks like this:

...
export const theme = {
  dark: {
    buttons: {
      button: {
        buttonStyle: {
          backgroundColor: colors.primary,
          borderRadius: 8,
          width: 275
        }
      },
      blockButton: {
        containerStyle: { width: "100%" },
        buttonStyle: {
          backgroundColor: colors.primary,
          borderRadius: 0
        }
      },
...

The only way to make this warning to away--aside from disabling the rule--is to use variables in lieu of text, like this:

<Text style={styles.emoticon}>{cleaner}</Text>

But that seems like a lot of work, that I'm loath to consider.

Any clues and/or advice would be most appreciated.

@Intellicode
Copy link
Owner

Intellicode commented Aug 27, 2020

Did you try the latest one, 3.9.1? It fixes this one #266

@kimfucious
Copy link
Author

kimfucious commented Aug 28, 2020

Hi @Intellicode,

Thanks for the suggestion.

I've just upgraded to 3.9.1, but unfortunately the problem remains.

@mjmasn
Copy link

mjmasn commented Sep 2, 2020

Similar issue:

export const Comp = () => {
  return (
    <Text>
      <Text>The quick brown fox jumped over the lazy dog</Text>{" "}
      {/* Whitespace(s) cannot be used outside of a <Text> tag   ^ */}
      <Text>The quick brown fox jumped over the lazy dog</Text>
    </Text>
  );
};

@Intellicode
Copy link
Owner

Similar issue:

export const Comp = () => {
  return (
    <Text>
      <Text>The quick brown fox jumped over the lazy dog</Text>{" "}
      {/* Whitespace(s) cannot be used outside of a <Text> tag   ^ */}
      <Text>The quick brown fox jumped over the lazy dog</Text>
    </Text>
  );
};

I could reproduce this test, let me see what I can do. If anyone has time, a PR would be appreciated else it would have to wait until I have time to look into the issue in detail.

@pke
Copy link

pke commented Nov 16, 2020

problem still exists in 3.10.0

@Crafter-Y
Copy link

Crafter-Y commented Oct 4, 2023

Getting the same Problem when declaring a custom Component:

const H1 = ({ children, style }: Props) => {
  return (
    <Text
      style={tw.style("text-4xl font-bold opacity-85 underline mb-4", style)}
    >
      {children}
    </Text>
  );
};

And then having something like this in my code:

<H1>Settings</H1>

I think this should not be an error, but I can understand why it is. Will there be a fix for that or is it intentional to throw an error there?

@karthikeyan-raju-focus
Copy link

Getting the same Problem when declaring a custom Component:

const H1 = ({ children, style }: Props) => {
  return (
    <Text
      style={tw.style("text-4xl font-bold opacity-85 underline mb-4", style)}
    >
      {children}
    </Text>
  );
};

And then having something like this in my code:

<H1>Settings</H1>

I think this should not be an error, but I can understand why it is. Will there be a fix for that or is it intentional to throw an error there?

@Crafter-Y did you find any solution for this error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants