首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让this.setState在React-Native中工作

如何让this.setState在React-Native中工作
EN

Stack Overflow用户
提问于 2020-06-13 08:56:09
回答 2查看 108关注 0票数 0

我正在努力让this.setState与react-native一起工作。我一直认为this.setState不是一个函数错误。我使用的是一个箭头函数,我认为它会使用封闭的作用域,不需要绑定到这个函数。但它并没有像预期的那样工作。我很感激能得到的任何帮助。

代码语言:javascript
运行
复制
   **
 * 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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-13 10:00:43

使用react hooks

代码语言:javascript
运行
复制
import React, { useState } from 'react'

const App: () => React$Node = () => {
  const [textValue, setTextValue] = useState('Original Text')

  buttonPress = () => setTextValue('New Text')
  ...
}
票数 1
EN

Stack Overflow用户

发布于 2020-06-13 09:02:21

如果要使用setState功能,则需要定义类。就像这样。

代码语言:javascript
运行
复制
class App extends React.Component {
  constructer(props) {
    super(props)

    this.state = {
     textValue: 'Original Text'
    };
  }

  buttonPress = () => this.setState({textValue: 'New Text'})

  render () {
    return (
      ...
    )
  }
}

如果您想使用该功能,请阅读文档。

https://reactjs.org/docs/hooks-intro.html

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62354334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档