[英]React Native - state won't change
I'm working on my first small game app.我正在开发我的第一个小游戏应用程序。
What I want to do is passing boolean GreenF to the Green component.我想要做的是将 boolean GreenF 传递给 Green 组件。 (this component is a green button that checks if greenFlash is true, then changes the color of the button - works ok). (此组件是一个绿色按钮,用于检查 greenFlash 是否为真,然后更改按钮的颜色 - 工作正常)。
But the greenF state won't change from false to true, then the Green component is not changed.但是greenF状态不会从false变为true,那么Green组件就不会改变。 I need GreenF to change whenever I need in the function play();我需要 GreenF 在函数 play() 中随时更改;
Right now greenF is undefined whenever I'm passing it to .现在,每当我将其传递给 .greenF 时,它都是未定义的。 I'm not sure if I'm missing something or having a problem understanding React state.我不确定我是否遗漏了什么或在理解 React 状态时遇到了问题。
This is just the beginning of my code so nothing is full, but just for your understanding.这只是我代码的开始,所以没有什么是完整的,但仅供您理解。
import React, {Component} from 'react';
import {StyleSheet, Text, View,TouchableOpacity, Button} from 'react-native';
import Green from './components/Green.js'
export default class App extends Component{
constructor (props){
super(props);
this.state = {
greenF: false
}
}
render() {
let score; // user score
let seq=[];//order of playing colors
let playerSeq=[]; //order of user pressing the colors
let round; // round number
let ok; //does the user pressed right
let win;
let compTurn;
let greenF;
/*
for (var i=0; i<5; i++) {
seq.push(Math.floor(Math.random()*4)+1);
}
*/
function play() {
seq=[1,2,3,1,4]; //just for test
compTurn=true;
round=1;
for (var i=0; i<round ; i++){
switch (i) {
case 1:
this.setState({ greenF: true })
break;
case 2:
//tbchanged
break;
case 3:
//tbchanged
break;
case 4:
//tbchanged
break;
}
compTurn=false;
}
}
return (
<View>
<Button
title='start'
color='black'
onPress={() => {
play();
}}>
</Button>
<Green greenFlash={greenF}> </Green> //GREENF IS UNDEFINED
</View>
);
}
}
You must take it from the state otherwise it is truly undefined:您必须从状态中取出它,否则它确实是未定义的:
<Green greenFlash={this.state.greenF} />
From React docs:来自 React 文档:
In addition to taking input data (accessed via this.props), a component can maintain internal state data (accessed via this.state).除了获取输入数据(通过 this.props 访问)之外,组件还可以维护内部状态数据(通过 this.state 访问)。 When a component's state data changes, the rendered markup will be updated by re-invoking render().当组件的状态数据发生变化时,渲染的标记将通过重新调用 render() 来更新。
That being said the play() method and the static variables you are trying to change are actually wrongly used/defined.话虽如此,您尝试更改的 play() 方法和静态变量实际上是错误使用/定义的。 All you have to do is to always use the state to trigger a re-rendering of the component:您所要做的就是始终使用状态来触发组件的重新渲染:
export default class App extends Component{
constructor (props){
super(props);
this.state = {
greenF: false,
score: 0,
seq: [1,2,3,1,4],
playerSeq: [],
round: 1,
}
}
play()
{
// I don't know exactly what you want to achieve here, but the idea
// was to move the play method outside the render method
seq=[1,2,3,1,4]; //just for test
compTurn=true;
round=1;
for (var i=0; i<round ; i++){
switch (i) {
case 1:
this.setState({ greenF: true })
break;
case 2:
//tbchanged
break;
case 3:
//tbchanged
break;
case 4:
//tbchanged
break;
}
compTurn=false;
}
}
render()
{
return (
<View>
<Button
title='start'
color='black'
onPress={this.play}>
</Button>
<Green greenFlash={this.state.greenF}> </Green>
</View>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.