代码之家  ›  专栏  ›  技术社区  ›  JorgeEstaAqui

如何使用react refs、回调模式更新状态

  •  1
  • JorgeEstaAqui  · 技术社区  · 7 年前

    Exercise 组件。

    addWorkoutRow 方法,不提供任何错误。按钮变回 Add Workout 但是模态不会关闭,状态也不会更新。

    我尝试使用setState回调,但没有成功。我很确定我的错误 const newRow 打电话给 setModalVisible 我只是没能退后一步看清楚。

    import React, {Component} from 'react';
    import {Modal, View, StyleSheet, TextInput, Text, TouchableOpacity} from 'react-native';
    
    const ExerciseItem = (name, sets, reps, weight) => (
        <View style={styles.exerciseItem}>
          <Text style={styles.itemInput}>{name}</Text>
          <Text style={styles.itemInput}>{sets}</Text>
          <Text style={styles.itemInput}>{reps}</Text>
          <Text style={styles.itemInput}>{weight}</Text>
        </View>
      );
    
    class Exercise extends Component {
      constructor() {
        super();
    
        this.state = {
          showCustomForm: false,
          showWeightsForm: !this.showCustomForm,
          modalVisible: false,
          rows: [],
          counter: 0,
        }
    
        this.addWorkoutRow = this.addWorkoutRow.bind(this);
      }
      setModalVisible(visible) {
        this.setState({modalVisible: visible})
      }
      addWorkoutRow() {
        const name = this.name._lastNativeText;
        const sets = this.sets._lastNativeText;
        const reps = this.reps._lastNativeText;
        const weight = this.weight._lastNativeText;
    
        const newRow = { name, sets, reps, weight };
        const rows = this.state.rows;
    
        rows.push(newRow);
    
        this.setState({ rows });
        this.setModalVisible(!this.state.modalVisible);
      }
      render() {
        const rows = []
    
        for (let i = 0; i < this.state.rows.length; i += i) {
          rows.push(<ExerciseItem 
            key={i} 
            name={this.state.rows[i].name} 
            sets={this.state.rows[i].sets}
            reps={this.state.rows[i].reps}
            weight={this.state.rows[i].weight}
          />);
        }
    
        return (
          <View style={styles.container}>
            <Text style={{fontSize: 20, color: '#fff', fontWeight: 'bold' }}>Workout</Text>
            <Modal
              style={styles.modalContainer}
              animationType="fade"
              transparent
              visible={this.state.modalVisible}
              onRequestClose={() => console.log('closed')}
              presentationStyle="overFullScreen"
            >
              <TextInput
                ref={(input) => { this.name = input; }} 
                style={styles.modalInputs}
                placeholderTextColor={'#fff'}
                placeholder={'Exercise Name'} />
              <TextInput
                ref={(input) => { this.sets = input; }} 
                style={styles.modalInputs}
                placeholderTextColor={'#fff'}
                placeholder={'Sets'} />
              <TextInput
                ref={(input) => { this.reps = input; }} 
                style={styles.modalInputs}
                placeholderTextColor={'#fff'}
                placeholder={'Reps'} />
              <TextInput
                ref={(input) => { this.weight = input; }} 
                style={styles.modalInputs}
                placeholderTextColor={'#fff'}
                placeholder={'Weight'} />
              <TouchableOpacity
                onPress={() => this.addWorkoutRow()}
                style={styles.modalBtn}>
                <Text style={styles.drkContent}>+</Text>
              </TouchableOpacity>
            </Modal>
            {rows}
            <View style={styles.btnContainer}>
              <TouchableOpacity 
                onPress={() => this.setModalVisible(!this.state.modalVisible)} 
                style={styles.btns}>
                <Text style={styles.drkContent}>Add Workout</Text>
              </TouchableOpacity>
            </View>
          </View>
        )
      }
    }
    
    export default Exercise
    

    {
        "name": "PresentApp",
        "version": "0.0.1",
        "private": true,
        "scripts": {
            "start": "node node_modules/react-native/local-cli/cli.js start",
            "test": "jest",
            "lint": "eslint ./components"
        },
        "dependencies": {
            "babel-eslint": "^8.2.1",
            "eslint": "^4.17.0",
            "eslint-config-airbnb": "^15.1.0",
            "eslint-config-prettier": "^2.9.0",
            "eslint-plugin-import": "^2.8.0",
            "eslint-plugin-jsx-a11y": "^5.1.1",
            "eslint-plugin-react": "^7.6.1",
            "prettier": "^1.10.2",
            "prop-types": "^15.6.0",
            "react": "16.0.0",
            "react-native": "0.50.4",
            "react-native-router-flux": "^4.0.0-beta.24",
            "react-native-sound": "^0.10.4"
        },
        "devDependencies": {
            "babel-jest": "21.2.0",
            "babel-preset-react-native": "4.0.0",
            "eslint-plugin-prettier": "^2.6.0",
            "firebase": "^5.0.4",
            "jest": "21.2.1",
            "react-test-renderer": "16.0.0"
        },
        "jest": {
            "preset": "react-native"
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   vahissan    7 年前

    在for循环中,你是这样递增的 i += i . 这将导致无限循环。这就是为什么你的组件被困在渲染。将循环增量更改为 i += 1

    推荐文章