0

I am trying to store react-native jsx element inside a variable while printing inside return method but I am getting error like this: Text strings must be rendered within a component. Below is my code in which I am getting error. Any help will be appreciated:

import { Text, View, Button } from 'react-native';
import React, { useState } from 'react';

const GameScreen = (props) => {

  const [num, setNum] = useState(0);
  const [randNum, setRandNum] = useState(0);
  const [currentNum, setCurrentNum] = useState('')
  // const [jsxVal, setJsxVal] = useState('')
  let jsxVal = '';

  const setRandomNum = (num, min, max) => {
    const randNum = Math.floor(Math.random() * (max - min)) + min
    setNum(num);
    setRandNum(randNum);
    console.warn("Number is:" + num + " and random number:" + randNum);
  }

  const checkNum = (num) => {
    if (num < randNum) {
      jsxVal = <Text>Your number {num} is lower than {randNum}</Text>;
      console.warn("if")
    } else if (num === randNum) {
      jsxVal = <Text>Your number {num} is greater than {randNum}</Text>;
      console.warn("else if")
    } else {
      jsxVal = <Text>Your number {num} is greater than {randNum}</Text>;
      console.warn("else")
    }
    console.warn("checkNum")
  }

  return (
    <View>
      {
        num === 0 ? setRandomNum(props.navigation.getParam('number'), 1, 100) : null
      }
      <Text>Value is :{num}</Text>
      <View style={{
        justifyContent: 'space-around',
        paddingHorizontal: 8,
        paddingVertical: 8,
        flexDirection: 'row'
      }}>
        <Button title="Lower" onPress={() => { checkNum(num) }} />
        <Button title="Greater" onPress={() => { checkNum(num) }} />
        {
          jsxVal === '' ? null : jsxVal 
        }
      </View>
    </View>
  );
}

export default GameScreen;
5
  • Try to initialize jsxVal variable with jsx syntax (or null) and not and empty string. Commented Oct 28, 2019 at 9:20
  • Why store at variable? you can make the jsx as return of checkNum function, and then call the fucntion itself Commented Oct 28, 2019 at 9:34
  • @HelloWorld, I believe the problem was with { jsxVal === '' ? jsxVal : null }, this returns empty string when the component renders for the first time, and hence the error. But you've already edited the question to the correct form. Did it work? Commented Oct 28, 2019 at 9:48
  • @VigneshVPai but null should not be a problem! Commented Oct 28, 2019 at 9:54
  • @HelloWorld I added an answer for that. Commented Oct 28, 2019 at 10:01

3 Answers 3

2

First, you can make the function returns the jsx itself:

 checkNum = (num) => {
   if (num < randNum) {
      return(<Text>Your number {num} is lower than {randNum}</Text>);
   } else if (num === randNum) {
     return(<Text>Your number {num} is greater than {randNum}</Text>);
   } else {
     return(<Text>Your number {num} is greater than {randNum}</Text>);
}}

Then, you call them method wherever you want:

 <Button title="Greater" onPress={() => { checkNum(num) }} />
 {this.checkNum(num)}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is the correct answer and the proper method.
1

You have to enclose your jsxVal into <Text> component e.g:

{
 jsxVal === '' ? null : <Text>{jsxVal}</Text> 
}   

It is because, initially you are using jsxVal = '', which read as text. That must be enclosed in <Text> component. When jsxVal get some value, in your case :

jsxVal = <Text>Your number {num} is greater than {randNum}</Text> 

then it can be rendered in <View> component.

3 Comments

Why you have used <Text> inside variable?
jsxVal = <Text>Your number {num} is greater than {randNum}</Text> This doesn't work when i put it inside <View>
it will work if you use : {jsxVal === '' ? null : <Text>{jsxVal}</Text> }. Kindly read complete solution, you will understand, why this error occurs.
1
 const [message, setMessage] = useState('')
 const checkNum = (num) => {
    if (num < randNum) {
      jsxVal = `Your number ${num} is lower than ${randNum}`;
      console.warn("if")
    ......
     setMessage(jsxVal)

Text strings must be rendered within a component. meaning that '< Text >' cannot be a parameter of Button , instead you should send a simple string value and save it in the component state

<View>
      {
        num === 0 ? setRandomNum(props.navigation.getParam('number'), 1, 100) : null
      }
      <Text>Value is :{num}</Text>
      <View style={{
        justifyContent: 'space-around',
        paddingHorizontal: 8,
        paddingVertical: 8,
        flexDirection: 'row'
      }}>
        <Button title="Lower" onPress={() => { checkNum(num) }} />
        <Button title="Greater" onPress={() => { checkNum(num) }} />
        {
          jsxVal === '' ? null : jsxVal 
        }
        <Text>{message}</Text>
      </View>
    </View>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.