2017-08-14 9 views
1

こんにちは私は2つのコンポーネントparentchildを反応させてきたそれらの両方が同じオブジェクトとオブジェクトにreduxstoreに接続されているサーバーからデータを取得asynchronouslycomponentWillReceiveProps()が子コンポーネントで実行されていませんか?

問題:新しい小道具の親のcomponentWillReceiveProps を受けた上を実行しますが、子供の取得componentWillReceivePropsは実行されていません

子コンポーネント

import React, {Component} from 'react'; 
import {bindActionCreators} from 'redux'; 
import {connect} from 'react-redux'; 


class Child extends Component{ 

    constructor(props) { 
    super(props); 
    this.handleUserClick = this.handleUserClick.bind(this) 

    } 

    handleUserClick(event){ 
    event.preventDefault(); 
    let userInfo = { 
     email: '[email protected]', 
     password: 'password', 
    }; 
    this.props.someAction(userInfo); // changes the store but on store change componentWillReceiveProps not executing 
    } 

    componentWillReceiveProps(nextProps){ 
    console.log(nextProps) 
    } 


render(){ 
    return (
     <div><form onSubmit={this.handleUserClick}></form></div> 
) 
    } 
} 

function matchDispatchToProps(dispatch) { 
    return bindActionCreators(
    { someAction: someAction}, dispatch) 
} 

function mapStateToProps (state){ 
    return { 
    dummyState: state.dummyState 
    }; 
} 

export default connect(mapStateToProps, matchDispatchToProps)(Child); 

私が実行取得されhandleUserClickをクリックしますが、サーバーchildcomponentWillReceivePropsからデータを受信する上で実行されていませんが、parentcomponentWillReceiveProps

私の親コンポーネント

を実行していますよ
import React from 'react'; 
import { connect } from 'react-redux'; 
import {bindActionCreators} from 'redux'; 

class Parent extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = {parentState: 0} 
    } 


    componentDidMount() { 
    this.props.someAction(); // same action called in child component 
    } 

componentWillReceiveProps(nextProps){ 
    // it gets executed and call child render function 
    if(nextProps.something === true){ 
     this.setState({parentState: 1 }) 
    } 
    if(nextProps.something === false){ 
     this.setState({parentState: -1}) 
    } 

    } 


    render() { 

    if(this.state.parentState == -1){ 
     return(
     <Child/> 
     ) 
    } 
    else{ 
     return(null) 
    } 
    } 
} 



function matchDispatchToProps(dispatch) { 
    return bindActionCreators(
    { someAction: someAction}, dispatch) //same action in child component called 
} 



function mapStateToProps (state){ 
    return { 
    dummyState: state.dummyState // //same state in child component called 
    }; 
} 

export default connect(mapStateToProps, matchDispatchToProps)(Parent); 

答えて

1

初めて子をレンダリングする場合(renderChildtrueに設定)、componentWillReceivePropsは呼び出されません(これは更新用です)。代わりにcomponentWillMountを調べる必要があります。

すでにtrueだったの状態をtrueにすると、何も起こりません。

+0

最新コード – ashwintastic

+0

子で 'componentWillMount'を使ってみてください。 –

+0

私はsetStateを使用し、 'componentWillMount'を使用してレンダリングを呼び出す必要はありません – ashwintastic

0

は、それはあなたの減速が更新されていない理由の

  1. のカップルかもしれないし、状態は同じままです。
  2. あなたのレデューサーは状態を更新していますが、コンポーネントの親コンポーネントと子コンポーネントは再描画されていません。
  3. 変更された小道具を子コンポーネントに渡しておらず、あなたがreduxに依存してレンダリングしている場合は、更新中の状態属性の名前を間違えた可能性があります。

アクションクリエイター、レデューサー、さらに少しのソースなど、より多くのsrcコードを提供した場合は、デバッグが容易になります。

+0

はコードを更新しました – ashwintastic

+0

親コンテナを見ると、子コンポーネントがある時点で潜在的にアンマウントされているように見えます。それは、子コンポーネントがアンマウントされ、論理のために再度マウントされた場合、コンポーネントWillReceivePropsがオフになっているのを見ていない理由です。 子コンポーネントにcomponentWillUnmount(){console.log( "Child is unmounted");}を追加し、呼び出された頻度を確認してください。これはあなたの問題かもしれません –

関連する問題