Node.js 如何使此.setState在React Native中工作
我正在努力使此.setState与react native一起工作。我一直得到这个。setState不是一个函数错误。我使用的是一个arrow函数,我认为它将使用封闭的作用域,并且没有必要绑定到这个函数。但它并没有像预期的那样发挥作用。我很感激能得到的任何帮助Node.js 如何使此.setState在React Native中工作,node.js,react-native,Node.js,React Native,我正在努力使此.setState与react native一起工作。我一直得到这个。setState不是一个函数错误。我使用的是一个arrow函数,我认为它将使用封闭的作用域,并且没有必要绑定到这个函数。但它并没有像预期的那样发挥作用。我很感激能得到的任何帮助 ** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local */ i
**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
Button,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
const App: () => React$Node = () => {
state = {
textValue: 'Original Text'
};
buttonPress = () => this.setState({textValue: 'New Text'})
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView>
<ScrollView
contentInsetAdjustmentBehavior="automatic"
style={styles.scrollView}>
<Header />
{global.HermesInternal == null ? null : (
<View style={styles.engine}>
<Text style={styles.footer}>Engine: Hermes</Text>
</View>
)}
<View style={styles.body}>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>{this.state.textValue}</Text>
<Button
onPress={buttonPress}
title="Learn More"
color="#841584"
accessibilityLabel="This is a button"
/>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Step One</Text>
<Text style={styles.sectionDescription}>
Edit <Text style={styles.highlight}>App.js</Text> to change this
screen and then come back to see your edits.
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>See Your Changes</Text>
<Text style={styles.sectionDescription}>
<ReloadInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Debug</Text>
<Text style={styles.sectionDescription}>
<DebugInstructions />
</Text>
</View>
<View style={styles.sectionContainer}>
<Text style={styles.sectionTitle}>Learn More</Text>
<Text style={styles.sectionDescription}>
Read the docs to discover what to do next:
</Text>
</View>
<LearnMoreLinks />
</View>
</ScrollView>
</SafeAreaView>
</>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
},
engine: {
position: 'absolute',
right: 0,
},
body: {
backgroundColor: Colors.white,
},
sectionContainer: {
marginTop: 32,
paddingHorizontal: 24,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
color: Colors.dark,
},
highlight: {
fontWeight: '700',
},
footer: {
color: Colors.dark,
fontSize: 12,
fontWeight: '600',
padding: 4,
paddingRight: 12,
textAlign: 'right',
},
});
export default App;
**
*示例React本机应用程序
* https://github.com/facebook/react-native
*
*@格式
*@flow严格本地
*/
从“React”导入React;
进口{
安全区域视图,
样式表,
滚动视图,
看法
文本,
状态栏,
按钮
}从“反应本机”;
进口{
标题,
学习重新链接,
颜色,
调试说明,
重新加载指令,
}来自“react native/Libraries/NewAppScreen”;
常量应用程序:()=>React$Node=()=>{
状态={
textValue:“原始文本”
};
buttonPress=()=>this.setState({textValue:'newtext'})
返回(
{global.hermistinternal==null?null:(
引擎:爱马仕
)}
{this.state.textValue}
第一步
编辑App.js以更改此设置
屏幕,然后回来查看您的编辑。
查看您的更改
调试
了解更多
阅读文档,了解下一步要做什么:
);
};
const styles=StyleSheet.create({
滚动视图:{
背景颜色:颜色。颜色较浅,
},
发动机:{
位置:'绝对',
右:0,,
},
正文:{
背景颜色:Colors.white,
},
节容器:{
玛金托普:32,
水平方向:24,
},
章节标题:{
尺寸:24,
重量:'600',
颜色:颜色,黑色,
},
章节说明:{
玛金托普:8,
尺码:18,
fontWeight:'400',
颜色:颜色,
},
亮点:{
重量:'700',
},
页脚:{
颜色:颜色,
尺寸:12,
重量:'600',
填充:4,
paddingRight:12,
textAlign:'右',
},
});
导出默认应用程序;
如果要使用设置状态功能,则需要类定义。像这样
类应用程序扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
textValue:“原始文本”
};
}
buttonPress=()=>this.setState({textValue:'newtext'})
渲染(){
返回(
...
)
}
}
如果您想使用该功能,请阅读文档
如果要使用设置状态功能,则需要类定义。像这样
类应用程序扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
textValue:“原始文本”
};
}
buttonPress=()=>this.setState({textValue:'newtext'})
渲染(){
返回(
...
)
}
}
如果您想使用该功能,请阅读文档
使用
使用
import React, { useState } from 'react'
const App: () => React$Node = () => {
const [textValue, setTextValue] = useState('Original Text')
buttonPress = () => setTextValue('New Text')
...
}