简体   繁体   English

function 作为 JSX - React Native

[英]function as JSX - React Native

I want this function to be displayed in JSX code我希望这个 function 显示在 JSX 代码中

function getValue() {
const QuestionChild = ['zara','whats up1','have u ever been in uk1']
const randomItem = QuestionChild[Math.floor(Math.random() * QuestionChild.length)];
console.log(randomItem)
return randomItem
}

Console log shows this result but can't be displayed in code控制台日志显示此结果但无法在代码中显示

Here is JSX:这是 JSX:

                <TouchableOpacity>
                <View style={styles.btnBack}><Text style={styles.smallwowo}>Back</Text></View> 
            </TouchableOpacity>

            <View style={styles.box}>
            <Text>{getValue}</Text>
            <TouchableOpacity onPress={()=> {getValue} }>
             <View style={styles.btn}><Text style={styles.wowo}>Next</Text></View>
             </TouchableOpacity>

When Next button will be clicked it should return randomly chosen string from Question array单击下一步按钮时,它应该从问题数组中返回随机选择的字符串

EDIT to use hooks and to separate getValue() from state-related concerns:编辑使用钩子并将getValue()与状态相关的问题分开:

Leave your getValue function the same as you originally had.让您的getValue function 与原来的相同。

Somewhere at the top of your component:在组件顶部的某处:

const [randomValue, setRandomValue] = useState(getValue());

Then somewhere below that create a new onPress handler:然后在下面的某个地方创建一个新的onPress处理程序:

const onGetNewValue = () => setRandomValue(getValue());

Then in your onPress statement:然后在您的onPress声明中:

<TouchableOpacity onPress={onGetNewValue}>

And where you actually display the value would be而你实际显示价值的地方是

<Text>{randomValue}</Text>

Old answer:老答案:

First, to actually display the value, you need to actually call the function, so use首先,要实际显示值,您需要实际调用 function,所以使用

<Text>{getValue()}</Text>

Next, to have the button press change the value, you need to store the value in a state, which will trigger a re-render.接下来,要让按钮按下更改值,您需要将值存储在 state 中,这将触发重新渲染。 There are different ways of doing this, but I would suggest in your constructor, create有不同的方法可以做到这一点,但我会在你的构造函数中建议,创建

this.state = { randomValue: getValue() }

Then in getValue do然后在getValue

...
const randomItem = QuestionChild[Math.floor(Math.random() * QuestionChild.length)];
this.setState({ randomValue: randomItem });

then instead, use然后改为使用

<Text>{this.state.randomValue}</Text>

Now, every time the button is clicked, a new value will be stored to state, and your component will be re-rendered with the new state value.现在,每次单击按钮时,都会将一个新值存储到 state,并且您的组件将使用新的 state 值重新渲染。

you should store some state -- whether via useState or via a stateful component -- and have the getValue function change that state.您应该存储一些 state——无论是通过useState还是通过有状态组件——并让getValue function 更改为 state。

Then you would do <TouchableOpacity onPress={getValue}> , not onPress={()=> {getValue} }然后你会做<TouchableOpacity onPress={getValue}> ,而不是onPress={()=> {getValue} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM