Reactコンポーネントで初期データを取得するためのディスパッチを呼び出す最良の方法は何ですか?私の理解は、レンダリングの前にComponentWillMountが呼び出されるということです。だから理論的には、私がComponentWillMountでディスパッチを呼び出すと、レンダリングしてからComponentDidMountを押すまでに、コンポーネントの小道具に自分のデータがあるはずです。私はそれを見ていないよ。React Redux Dispatch
レンダリングが2回呼び出されているのが見えています。コンポーネントが初期化されるときにレンダリングが最初に実行されると、私は小道具のデータにアクセスできません。また、ディスパッチは2番目のレンダリングまで実際に呼び出されないようです。私は基本的に、最初にコンポーネントを設定するときに、ディスパッチを呼び出す最良の方法についていくつかの光を当てたいと考えています。私は本質的に次のようなことをしようとしています。コンテナコンポーネントを使用してデータを送出し、それを小道具として子コンポーネントに渡します。しかし、私はまた、ContainerComponent内のいくつかの状態変数を初期化し、それらをChildComponentにpropsとして渡したいと思っています。問題は、私が初期化したい状態変数が、ディスパッチから返されたデータに依存し、理想的には、ComponentWillMountまたはComponentDidMountで初期化を行うということです。ここで
import React from 'react';
import axios from 'axios';
import { connect } from 'react-redux';
import ChildComponent from './ChildComponent.js';
import { getTransactionsAll } from '../actions/actions.js';
class ContainerComponent extends React.Component {
constructor() {
super();
this.state = {
acctList:[],
acctChecked:[],
categoryList:[]
}
}
componentWillMount() {
console.log("componentWillMount entered");
this.props.get_data();
console.log(this.props.searchProps.transactions_all);//this is undefined meaning the dispatch has not assigned the data yet...??
}
componentDidMount() {
console.log("componentDidMount entered");
console.log(this.props.searchProps.transactions_all);//this is undefined meaning the dispatch has not assigned the data yet...??
}
render() {
console.log("TransactionManagerContainer render entered");
console.log(this.props.searchProps.transactions_all);//this is undefined the first time around meaning the dispatch has not assigned the data yet...??, but is defined on the second call to render after the dispatch has actually occurred...
return <ChildComponent
data={this.props.searchProps.data}/>;
}
const mapStateToProps = (state) => ({
searchProps: state.searchProps
});
export default connect(mapStateToProps, {getTransactionsAll})(TransactionManagerContainer);
は、状態を割り当て、私の減速である:ここでは
import { combineReducers } from 'redux'
import {GET_TRANSACTIONS } from '../actions/actions.js'
import {GET_TRANSACTIONS_ALL } from '../actions/actions.js'
const INITIAL_STATE = { defaultYear: 2016, transactions: []};
function get_transactions(state = INITIAL_STATE, action) {
// console.log("this is in the reducer: get_transactions");
// console.log(action);
switch(action.type) {
case GET_TRANSACTIONS:
// return { ...state, transactions: action.payload };
return Object.assign({}, state, {
transactions: action.payload,
selectedYear: action.selectedYear
})
default:
return state;
}
}
function get_transactions_all(state = INITIAL_STATE, action) {
console.log("this is the value of action in the reducer: get_transactions_all");
console.log(action);
switch(action.type) {
case GET_TRANSACTIONS_ALL:
// return { ...state, transactions: action.payload };
return Object.assign({}, state, {
transactions_all: action.payload
})
console.log("this is the value of state in the reducer after being set");
console.log(state);
default:
return state;
}
}
const rootReducer = combineReducers({
//stateProps: get_transactions,
searchProps: get_transactions_all
})
export default rootReducer
は私のアクションは、次のとおりです。
What is the best way to call a dispatch to get initial data on a React component?
行き方:
import axios from 'axios';
export const GET_TRANSACTIONS = 'GET_TRANSACTIONS';
export function getTransactions(year) {
return function(dispatch) {
axios.get(`http://localhost:3001/api/transfilter?year=${year}&grouping=2`)
.then(response => {
dispatch({
type: GET_TRANSACTIONS,
payload: response.data,
selectedYear: year
});
})
.catch((error) => {
console.log(error);
})
}
}
export const GET_TRANSACTIONS_ALL = 'GET_TRANSACTIONS_ALL';
export function getTransactionsAll(year) {
return function(dispatch) {
axios.get(`http://localhost:3001/api/trans?limit=20`)
.then(response => {
dispatch({
type: GET_TRANSACTIONS_ALL,
payload: response.data
});
})
.catch((error) => {
console.log(error);
})
}
}
我々はcomponentDidMountでフェッチAPIデータメソッドを呼び出しています。 HTMLテンプレートでコンポーネントをどのように使用していますか? –
フェッチが機能します。すべてが機能します。私はちょうどそれを行うための最善の方法を知り、ディスパッチ "ライフサイクル"をよりよく理解したいと思っています。レンダリングが2回呼び出され、2回目のレンダーが呼び出されるまでデータが利用できないので、フェッチが実際に呼び出されるのはいつかです。 –
ここで 'searchProps'を渡しています。このコンテナの親コンポーネントと 'get_data'関数を表示してください。 – azium