When to use componentWillReceiveProps lifecycle method?

后端 未结 5 968
时光取名叫无心
时光取名叫无心 2020-12-04 23:51

I am new to React/Redux and have a problem with state.

TrajectContainer.jsx

class TrajectContainer extends React.Component {
    con         


        
5条回答
  •  一个人的身影
    2020-12-05 00:25

    Update

    componentDidUpdate()

    should now be used rather than componentWillReceiveProps

    also see an article from gaearon re writing resilient components

    There are two potential issues here

    1. Don't reassign your props to state that is what you are using redux for pulling the values from the store and returning them as props to your component

    Avoiding state means you no longer need your constructor or life-cycle methods. So your component can be written as a stateless functional component there are performance benefits to writing your component in this way.

    1. You do not need to wrap your action in dispatch is you are passing mapDispatcahToProps. If an object is passed, each function inside it is assumed to be a action creator. An object with the same function names, but with every action creator wrapped into a dispatch will be returned

    Below is a code snippet that removes the state from your component and relies on the state that has been returned from the redux store

    import React from "react";
    import { connect } from "react-redux";
    
    const TrajectContainer = ({ trajects, addTraject }) => (
    	

    Trajects

    {trajects.map(traject => )}
    ); const mapStateToProps = ({ trajects }) => ({ trajects }); export default connect( mapStateToProps, { addTraject })(TrajectContainer);

提交回复
热议问题