Can I update a component's props in React.js?

后端 未结 6 1082
萌比男神i
萌比男神i 2020-11-27 10:15

After starting to work with React.js, it seems like props are intended to be static (passed in from the parent component), while state changes base

6条回答
  •  情书的邮戳
    2020-11-27 11:11

    Trick to update props if they are array :

    import React, { Component } from 'react';
    import {
      AppRegistry,
      StyleSheet,
      Text,
      View,
      Button
    } from 'react-native';
    
    class Counter extends Component {
      constructor(props) {
        super(props);
          this.state = {
            count: this.props.count
          }
        }
      increment(){
        console.log("this.props.count");
        console.log(this.props.count);
        let count = this.state.count
        count.push("new element");
        this.setState({ count: count})
      }
      render() {
    
        return (
          
            { this.state.count.length }
            

提交回复
热议问题