我有以下 React 组件:
import React from 'react';
import PropTypes from 'prop-types';
import {connect} from 'react-redux';
import {loadJobTitleSkills} from '../../actions/jobTitleSkillsActions';
import SkillList from './SkillList';
class SkillPage extends React.Component {
componentDidMount() {
if (this.props.user_positions[0]) {
this.props.dispatch(loadJobTitleSkills(this.props.user_positions[0].job_title_id));
}
}
render() {
return (
<div>
<SkillList skills={this.props.job_title_skills} />
</div>
);
}
}
SkillPage.propTypes = {
job_title_skills: PropTypes.array.isRequired,
user_positions: PropTypes.array.isRequired,
};
const mapStateToProps = state => {
return {
job_title_skills: state.job_title_skills,
user_positions: state.user_positions
};
};
export default connect(mapStateToProps)(SkillPage);
state.user_positions={}当组件呈现为前一个组件时,它是一个提交state.user_positions. 在 SkillPage 组件呈现之后, state.user_positions 被填充,我在 Redux chrome 扩展中验证了这一点。
问题是这个 SkillPage 组件不会自动重新渲染。我希望组件自动检测state.user_positions已更改并重新运行 componentDidMount,然后将 dispatch loadJobTitleSkills()。
我在这里做错了什么?为什么每当 state.user_positions 被修改时我的组件不重新渲染?
userPositionReducer.js:
import * as types from '../actions/actionTypes';
const initialState = {}
export default function userPositionReducer(state = initialState, action) {
switch (action.type) {
case types.CREATE_USERPOSITION_SUCCESS:
return action.user_position
case types.LOAD_USERPOSITION_SUCCESS:
return action.user_positions
default:
return state;
}
}
jobTitleSkillsReducer.js:
import * as types from '../actions/actionTypes';
const initialState = []
export default function jobTitleSkillsReducer(state = initialState, action) {
switch (action.type) {
case types.LOAD_JOBTITLESKILLS_SUCCESS:
return action.job_title_skills
default:
return state;
}
}
更新
通过将以下内容添加到我的 SkillPage 组件并删除 componentDidMount(),我能够让它“工作”
componentDidUpdate() {
if (this.props.user_positions[0] && this.props.job_title_skills.length === 0) {
this.props.dispatch(loadJobTitleSkills(this.props.user_positions[0].job_title_id));
}
}
这感觉超级hacky。有一个更好的方法吗?
更新 2
Working with this added:
componentWillReceiveProps(nextProps) {
if (nextProps.user_positions && nextProps.user_positions.length > 0) {
if (this.props.user_positions[0] !== nextProps.user_positions[0]) {
this.props.dispatch(loadJobTitleSkills(nextProps.user_positions[0].job_title_id));
}
}
}
这个解决方案仍然感觉超级hacky。
更新 3 - 添加动作创作者
jobTitleSkillsActions.js
import * as types from './actionTypes';
import JobTitleSkillsApi from '../api/JobTitleSkillsApi';
export function loadJobTitleSkillsSuccess(job_title_skills) {
return {type: types.LOAD_JOBTITLESKILLS_SUCCESS, job_title_skills};
}
export function loadJobTitleSkills(job_title_id) {
return function(dispatch) {
return JobTitleSkillsApi.getAllJobTitleSkills(job_title_id).then(skills => {
dispatch(loadJobTitleSkillsSuccess(skills));
}).catch(error => {
throw(error);
});
};
}
UserPositionActions.js
import * as types from './actionTypes';
import userPositionsApi from '../api/UserPositionsApi';
export function createUserPositionSuccess(user_position) {
return {type: types.CREATE_USERPOSITION_SUCCESS, user_position};
}
export function loadUserPositionSuccess(user_positions) {
return {type: types.LOAD_USERPOSITION_SUCCESS, user_positions};
}
export function loadUserPositions() {
// console.log('actions: loadUserPosition')
return function(dispatch) {
return userPositionsApi.getAllUserPositions().then(user_positions => {
dispatch(loadUserPositionSuccess(user_positions));
}).catch(error => {
throw(error);
});
};
}
export function createUserPosition(user_position) {
// console.log('actions: createUserPosition')
return function (dispatch) {
return userPositionsApi.createUserPosition(user_position).then(responseUserPosition => {
dispatch(createUserPositionSuccess(responseUserPosition));
return responseUserPosition;
}).catch(error => {
throw(error);
});
};
}