【问题标题】:Properties order of the state changes on submit handle提交句柄上状态更改的属性顺序
【发布时间】:2020-09-13 20:25:28
【问题描述】:

我有这段代码,在本机反应的 ChangeText 上,我在状态项中插入多个字段值。出于某种原因,控制台日志中的输出显示字母顺序。由于逻辑是,在 ChangText 上,函数 EnterValue 将值插入项目状态。它找到属性名称,然后将其与文本值匹配,我认为在此过程中出现了问题。请看下面的代码

import { StatusBar } from "expo-status-bar";
import React from "react";
import { StyleSheet, Text, View, Button, TextInput } from "react-native";

class App extends React.Component {
  constructor() {
    super();
    this.state = {
      step: 1,
      TotalItem: [],
      item: {
        Brand: null,
        Quantity: null,
        Instructions: null,
      },
    };
  }

  // remember this logig since take names, it adds property values in alphabetical order, and thus change the order
  EnterValue = (name) => {
    return (text) => {
      this.setState((prevState) => ({
        item: { ...prevState.item, [name]: text },
      }));
    };
  };

  submit = (e) => {
    e.preventDefault(e);

    const { step } = this.state;
    this.setState({
      step: step + 1,
    });

    this.setState(
      {
        TotalItem: [...this.state.TotalItem, this.state.item],
      },
      () => {
        console.log("updated state", this.state.TotalItem);
      }
    );
  };

  render() {
    return (
      <View style={{ flex: 1, margin: 20 }}>
        <TextInput
          placeholder="enter name"
          onChangeText={this.EnterValue("Brand")}
          style={{ borderWidth: 2, borderColor: "skyblue", margin: 20 }}
        />

        <TextInput
          placeholder="Quantity"
          onChangeText={this.EnterValue("Quantity")}
          style={{ borderWidth: 2, borderColor: "skyblue", margin: 20 }}
        />

        <TextInput
          placeholder="Instructions"
          onChangeText={this.EnterValue("Instructions")}
          style={{ borderWidth: 2, borderColor: "skyblue", margin: 20 }}
        />

        <View style={styles.container}>
          <View style={{ flex: 1 }}>
            <Button
              style={{ flex: 1, backgroundColor: "red" }}
              title="add"
              onPress={this.add}
            />
          </View>
          <View style={{ flex: 1 }}>
            <Button
              style={{ flex: 1, backgroundColor: "blue" }}
              title="submit"
              onPress={this.submit}
            />
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "center",
  },
});

export default App;

【问题讨论】:

  • 您能否添加一个实际输出与预期输出的示例?
  • 预期应该是我的状态项目。并且它的属性应该按顺序出现,就像在状态中显示的那样。但是在提交后的console.log中,属性按字母顺序显示,如品牌、说明、数量
  • 那是因为你在 JSX 上执行函数
  • 嘿,找到答案了吗?

标签: javascript reactjs react-native redux


【解决方案1】:

改变

onChangeText={this.EnterValue("Quantity")}
onChangeText={this.EnterValue("Brand")} 
onChangeText={this.EnterValue("Instructions ")} 

onChangeText={() => this.EnterValue("Quantity")}
onChangeText={() => this.EnterValue("Brand")} 
onChangeText={() => this.EnterValue("Instructions ")} 

【讨论】:

  • 抱歉回复晚了。但这不会填充状态。我刚检查过。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多