React Part of State ว่างเปล่าภายใน Render

ฉันมีแอป react / Redux ปัญหาคือฉันต้องการแสดงรายการองค์ประกอบในสถานะของส่วนประกอบของฉัน และรายการนี้ว่างเปล่าเสมอเมื่อมีการดำเนินการวิธีการเรนเดอร์และฉันไม่สามารถใช้งานได้

องค์ประกอบนี้มาจากการเรียก API

นี่คือองค์ประกอบของฉัน:

class PeopleView extends Component {

  static propTypes = {
    people: PropTypes.array,
    view: PropTypes.string
  }

  static stateToProps = state => ({
    people: state.properties.items || [],
    sortedPeople: sortBy(state.people.items || [], 'birthday'),
  })

  constructor(props) {
    super(props);
    const { people } = props;
    const sortedPeople = sortBy(people, 'birthday');

    this.state = {
      people: people,
      sortedPeople: sortedPeople,
      collapsed: true
    };
  }    

  toggleCollapsed = (e) => {
    this.setState({collapsed: !this.state.collapsed});
  }

  deleteHandler = (id) => {
    console.log("Person Id", id)    
  }

  render() {

    // THIS IS EMPTY: this.state.people = [] 
    // THIS IS NOT EMPTY: this.props.people = [{...}, {...}, {...}, ...]

    const sortedPeople = sortBy(this.props.people, 'createdAt');
    const sortedPeopleElements = this.props.sortedPeople.map((p, i) => {
      return <li key={p.id}>
        <Person index={i} id={p.id} name={p.name} onDelete={this.deleteHandler} />              
      </li>
    });    

    return (
      <div>
        <ul>
         {sortedPeopleElements}                  
        </ul>
      </div>
    );
  }
}

export default connect(PeopleView);

รายการผู้คนในสถานะจะว่างเปล่าเสมอในวิธีการเรนเดอร์ แต่รายการผู้คนในอุปกรณ์ประกอบฉากนั้นใช้ได้เสมอ ทำไมเป็นเช่นนี้?

ฉันจะตั้งค่าสถานะให้ทำงานกับการเรนเดอร์และเริ่มลบบุคคลออกจากรายการนี้ได้อย่างไร


person danielrvt    schedule 18.08.2018    source แหล่งที่มา
comment
ไม่มีพารามิเตอร์ใน connect()   -  person xadm    schedule 18.08.2018
comment
@xadm ฉันได้รับสิ่งนี้ Uncaught ReferenceError: stateToProps is not defined เมื่อเปลี่ยนเป็นสิ่งนี้: เชื่อมต่อ(stateToProps)(PeopleView);   -  person danielrvt    schedule 18.08.2018
comment
เพราะคุณจะต้องสร้างวิธีการ stateToProps   -  person Nikhil Thakur    schedule 18.08.2018
comment
เหตุใดจึงกำหนดให้เป็นแบบคงที่ ย้ายออกจากตัวส่วนประกอบ   -  person xadm    schedule 18.08.2018


คำตอบ (2)


โดยทั่วไปมีปัญหาเล็กน้อยในโค้ดของคุณ

  1. this.state.people ได้รับการเตรียมใช้งานในตัวสร้างเพียงครั้งเดียวจากค่า this.props.people ซึ่งโดยค่าเริ่มต้นคือ undefined หากคุณไม่ส่งค่า โปรดทราบว่าคุณไม่ได้ใช้ this.state.people ที่ใดก็ได้ในเมธอด render ของคุณ
  2. คุณกำลังผสมสถานะองค์ประกอบของ React และสถานะของ Redux สถานะของ Redux ถูกส่งผ่านไปยัง React เป็นอุปกรณ์ประกอบฉาก (โดยทั่วไปผ่านฟังก์ชันการทำแผนที่ที่เรียกว่า mapStateToProps คุณสามารถเรียกมันว่าอะไรก็ได้ที่คุณต้องการ) ฟังก์ชันการทำแผนที่นี้ต้องผ่านเพื่อเชื่อมต่อ (mapStateToProps) (PeopleView) ฟังก์ชันการเชื่อมต่อของ react-redux คือฟังก์ชันแกง
  3. ตามธรรมเนียมแล้ว คุณไม่จำเป็นต้องทำให้ stateToProps เป็นเมธอดสแตติกของคลาสคอมโพเนนต์ โดยปกติแล้วจะเป็นระดับบนสุด (หรือขอบเขตไฟล์) สำหรับฟังก์ชันนั้น

แจ้งให้เราทราบหากฉันสามารถชี้แจงคำตอบของฉันเพิ่มเติมได้

person light.wave    schedule 18.08.2018

ที่นี่ในตัวสร้างของคุณ:

constructor(props) {
    super(props);

/* Remove { } this brackets from the const variable and also you need props.people not props in it */
    const people  = props.people; 
    const sortedPeople = sortBy(people, 'birthday');

    this.state = {
      people: people,
      sortedPeople: sortedPeople,
      collapsed: true
    };
  }  
person Nikhil Thakur    schedule 18.08.2018