こんにちは私は2つのコンポーネントparent
とchild
を反応させてきたそれらの両方が同じオブジェクトとオブジェクトにredux
store
に接続されているサーバーからデータを取得asynchronously
componentWillReceiveProps()が子コンポーネントで実行されていませんか?
問題:新しい小道具の親の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
をクリックしますが、サーバーchild
componentWillReceiveProps
からデータを受信する上で実行されていませんが、parent
componentWillReceiveProps
私の親コンポーネント
を実行していますよ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);
最新コード – ashwintastic
子で 'componentWillMount'を使ってみてください。 –
私はsetStateを使用し、 'componentWillMount'を使用してレンダリングを呼び出す必要はありません – ashwintastic