【问题标题】:Input value is not seen completely, why?输入值看不到完整,为什么?
【发布时间】:2018-10-28 16:39:31
【问题描述】:

所以,几乎一切都按原样工作,问题是当我在输入中输入内容时,它不会完全更新 redux 状态。示例:如果我输入 ABC,它会像 AB 一样通过 axios.post() 将其发送到服务器...如果我输入 BEER,它将发送 BEE ...它看不到最后一个字母,或者如果我在我的设备,如果它是输入中的最后一件事,它不会看到整个单词....

有什么建议吗?

提前致谢。

  class AddressScreen extends Component {
      state = {
        usersNickOrName: "",
        usersAddress: "",
        usersPhoneNumber: ""
      };

      componentWillUpdate() {
    this.props.deliveryInfo(
      this.state.usersNickOrName,
      this.state.usersAddress,
      this.state.usersPhoneNumber
    );
  }

      onPressHandler = () => {


        let uid = this.props.uid;

        axios.post(
          `.../users/${uid}/info.json`,
          {
            nameOrNick: this.props.name,
            address: this.props.address,
            phoneNum: this.props.phoneNum
          }
        );
        this.props.navigator.pop();
      };

      render() {
        return (
          <View style={styles.container}>

            <AnimatedForm delay={100} distance={10}>
              <AnimatedInput
               onChangeText={text => {
                  this.setState({ usersNickOrName: text });
                }}
              />
              <AnimatedInput
                onChangeText={text => {
                  this.setState({ usersAddress: text });
                }}
              />
              <AnimatedInput
                onChangeText={text => {
                  this.setState({ usersPhoneNumber: text });
                }}
              />

              <Animated.View style={styles.buttonView}>
                <TouchableOpacity
                  style={styles.button}
                  onPress={this.onPressHandler}
                >
                  <Text style={{ color: "#fff" }}>Dodaj Info</Text>
                </TouchableOpacity>
              </Animated.View>
            </AnimatedForm>
          </View>
        );
      }
    }



    const mapStateToProps = state => {
      return {
        name: state.usersNickOrName,
        address: state.usersAddress,
        phoneNum: state.usersPhoneNumber,
        uid: state.userUid
      };
    };

    const mapDispatchToProps = dispatch => {
      return {
        deliveryInfo: (usersName, usersAddress, phoneNum) =>
          dispatch(deliveryInfo(usersName, usersAddress, phoneNum))
      };
    };

    export default connect(
      mapStateToProps,
      mapDispatchToProps
    )(AddressScreen);

【问题讨论】:

    标签: javascript reactjs react-native react-redux


    【解决方案1】:

    您当前正在使用componentWillUpdate 中的旧状态。改用下一个状态。

    componentWillUpdate(nextProps, nextState) {
      this.props.deliveryInfo(
        nextState.usersNickOrName,
        nextState.usersAddress,
        nextState.usersPhoneNumber
      );
    }
    

    【讨论】:

    • 请注意 componentWillUpdate 已被弃用 reactjs.org/docs/…
    • 谢谢。当使用 nextState 时,一切都很好......关于折旧......我应该怎么做?如何在不使用 componentWillUpdate 的情况下实现相同的效果?
    • @FatBoyGebajzla componentDidUpdate 未被弃用,因此您可以使用它。
    • @FatBoyGebajzla 如果您决定改用componentDidUpdate,那么您的状态将会更新,因此您应该再次使用this.state.usersNickOrName 等。
    • 再次感谢您!改变了它,它工作得很好。干杯伙伴!
    猜你喜欢
    • 1970-01-01
    • 2022-08-14
    • 2013-01-28
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2013-05-12
    相关资源
    最近更新 更多