【发布时间】:2017-01-06 21:53:46
【问题描述】:
调用 dispatch 以获取 React 组件的初始数据的最佳方式是什么。我的理解是在渲染之前调用了 ComponentWillMount。所以理论上,如果我在 ComponentWillMount 上调用 dispatch,当我点击渲染然后 ComponentDidMount 时,我应该将我的数据放在组件的 props 中,对吗?我没看到。
我看到 render 被调用了两次,并且在第一次初始化组件时,我无法访问 props 中的数据。在第二次渲染之前,似乎调度实际上并没有被调用。我基本上希望在最初设置组件时对调用调度的最佳方式有所了解。我实际上是在尝试执行以下操作,其中我使用容器组件从调度中获取数据,然后将其作为道具传递给子组件。但是我也想在ContainerComponent中初始化一些状态变量,然后将它们作为props传递给ChildComponent。问题是我要初始化的状态变量取决于从调度返回的数据,理想情况下我会在 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
这是我的行动:
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);
})
}
}
【问题讨论】:
-
在 react/nuclearjs 项目中,我们在 componentDidMount 中调用 fetch API 数据方法,这样应该可以工作。您能告诉我们您是如何在 HTML 模板中使用该组件的吗?
-
抓取有效。一切正常。我只想知道最好的方法并更好地了解调度“生命周期”。就像实际何时调用 fetch 一样,因为它似乎不会立即调用,因为 render 被调用了两次,并且数据在第二次调用 render 之前不可用。
-
你在哪里经过
searchProps。您应该显示此容器父组件和您的get_data函数 -
进行编辑以包含所有文件(操作、reducer 等)。我正在从我的 reducer 传递 searchProps。
-
我想我误解了你的问题。您是在问为什么您的数据只存在于第二次渲染中?
标签: reactjs react-redux