不变违规:对象作为 React 子对象无效 (React Native)

1 javascript reactjs react-native

我正在尝试实现一个简单的功能,onPress即应该添加一个按钮placeName来放置数组并将其显示在视图中,但我似乎收到错误,请帮助

这是我的代码,

export default class App extends Component{

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

  onChangeName = (val) => {
    this.setState({
      placeName: val
    })
  }

  placeSubmitHandler = () => {
    if(this.state.placeName === "") {
      alert('enter something');
    } else {
      this.setState(prevState => {
        return {
          places: prevState.places.concat(prevState.placeName)
        }
      })
    }
  }

  render() {
    const placesOutput = this.state.places.map((place, i) => (
        <Text key={i}>{place}</Text>
    ));
    return (
      <View style={styles.container}>
        <View style={styles.inputContainer}>
            <TextInput 
            style={{width: 300}}
            value={this.state.textInput}
            placeholder='enter anything'
            onChange={this.onChangeName}
            style={styles.placeInput}
            />
            <Button title='Add' style={styles.placeButton} onPress={this.placeSubmitHandler}/>
        </View>
        <View>
          {placesOutput}
        </View>
      </View>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误,

Invariant Violation: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, _dispatchListeners, _dispatchInstances, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, nativeEvent, isDefaultPrevented, isPropagationStopped}). If you meant to render a collection of children, use an array instead.
    in RCTText (at Text.js:154)
    in TouchableText (at Text.js:278)
    in Text (at App.js:48)
    in RCTView (at View.js:45)
    in View (at App.js:62)
    in RCTView (at View.js:45)
    in View (at App.js:51)
    in App (at renderApplication.js:35)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:34)
Run Code Online (Sandbox Code Playgroud)

我是本地反应新手,所以我对此一无所知。

Mil*_*ore 6

在您中,<TextInput>您使用的是onChangeprop 而不是onChangeText,因此您尝试在 中渲染对象<Text>。只需更改该道具就可以了

  <TextInput
    style={styles.placeInput}
    value={this.state.textInput}
    placeholder="enter anything"
    onChangeText={this.onChangeName}
  />
Run Code Online (Sandbox Code Playgroud)

另请注意,您正在复制其style属性。统一它们或将它们作为数组传递

style={[styles.placeInput, {width: 300}]}
Run Code Online (Sandbox Code Playgroud)