【问题标题】:React Native: Props Warning, invalid prop valueReact Native:道具警告,无效的道具值
【发布时间】:2019-06-11 15:58:25
【问题描述】:

我是新来的本地反应, 我正在构建一个示例应用程序,当向 TextInput 字段输入数据时,我收到了警告。 我尝试在 Android 模拟器和我的 Pocophone f1 设备上运行它并得到相同的结果。 我使用 VS Code 作为我的 IDE。 我正在 Ubuntu 18.04 上开发
任何人都可以帮忙吗? 这些是应用程序的屏幕截图

  1. 我正在输入的数据。

  2. 警告 Iget

这是我的代码

        /**
     * Sample React Native App
     * https://github.com/facebook/react-native
     *
     * @format
     * @flow
     */

    import React, {Component} from 'react';
    import {Platform, StyleSheet, Text, View, TextInput, Button} from 'react-native';

    //type Props = {};
    export default class App extends Component {

      state = {
        placeName: "",
        places: []
      }


      placeNameChangedHandler = (event) => {
        this.setState({
          placeName: event
        });
      }

      placeNameSubmitHandler = () => {
        if (this.state.placeName === ""){
          return;
        }
        this.setState(prevState => {
          return {
            places: prevState.places.concat(prevState.placeName)
          };
        });
      };

      render() {
        const placesOutput = this.state.places.map(place => (
          <Text>{place}</Text>
        ));
        return (
          <View style={styles.container}>

            <View style={styles.inputContainer}>
            <TextInput
            style={{width: 200, borderColor: "black", borderWidth: 1}}
            placeholder="Place Name"
            value={this.state.placeName} 
            onChangeText={this.placeNameChangedHandler} 
            style={styles.PlaceInput} />
            <Button title="Add"  style={styles.placeButton} onPress={this.placeNameChangedHandler} />
            </View>
            <View>
                {placesOutput}
            </View>
          </View>
        );
      }
    }

    const styles = StyleSheet.create({
      container: {
        flex: 1,
        padding: 26,
        justifyContent: 'space-between', 
        alignItems: 'center',
        backgroundColor: '#F5FCFF', 
      },
      inputContainer:{
        width: "100%",
        flexDirection: "row",
        justifyContent: "space-between",
        alignItems: 'center'
      },
      PlaceInput:{
    width: "70%"
      },
      placeButton:{
    width: "30%"
      }

    });

我按照以下答案中的建议更改了代码,但出现如下屏幕所示的错误

【问题讨论】:

    标签: react-native warnings textinput


    【解决方案1】:

    如果您要在placeNameChangedHandler 函数中记录“事件”,您会发现它是一个对象,而不仅仅是您要查找的字符串值。因此,您将状态设置为完整的事件对象,然后尝试在屏幕上呈现它。

    您需要对对象进行解构以获取您要查找的字符串值。

      placeNameChangedHandler = (event) => {
        this.setState({
          placeName: event.target.value
        });
      }
    

    【讨论】:

    • 谢谢,我按照您的建议更改了代码,并收到一条错误消息:未定义不是对象(正在评估 'event.target.value')我在原始问题中添加了一个打印屏幕,只有当我按下添加按钮时才会出现原始警告凸轮
    【解决方案2】:

    我发现了问题:在 **onPress 事件处理程序上,我调用了错误的函数,对不起,我西了 **

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多