警告:propType 失败:未在“DimensionPicker”中指定必需的道具“dimensionName”。检查 `Connect(DimensionPicker)` 的渲染方法
Warning: Failed propType: Required prop `dimensionName` was not specified in `DimensionPicker`. Check the render method of `Connect(DimensionPicker)`
我有以下 Redux+React 组件
import {PropTypes, React, Component} from 'react';
import Select from 'react-select';
class DimensionPicker extends Component {
componentDidMount() {
const {onLoad} = this.props;
onLoad();
}
render() {
const {onChange, attributeList, currentAttribute} = this.props;
return (
<div>
<Select value={currentAttribute} options={attributeList} onChange={onChange} />
</div>
)
}
}
DimensionPicker.propTypes = {
dimensionName: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
attributeList: PropTypes.arrayOf(PropTypes.shape({
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired
}).isRequired).isRequired,
currentAttribute: PropTypes.string.isRequired
}
export default DimensionPicker;
和以下容器组件
import React from 'react';
import DimensionPickerActions from '../actions/DimensionPickerActions';
import {connect} from 'react-redux';
import DimensionPicker from './controls/DimensionPicker.jsx';
const mapStateToProps = (state) => {
return {
dimensionName: state.dimensionName,
attributeList: state.attributeList,
currentAttribute: state.currentAttribute
}
}
const mapDispatchToProps = (state) => {
return {
onChange: (newValue) => {
dispatch(updateAttributeSelection(newValue));
},
onLoad: () => {
dispatch(fetchDimensionAttributes(state.dimensionName));
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(DimensionPicker);
我还有一个填充初始状态的减速器
// define the state tree for the dimenion picker.
const initialState = {
dimenisionName: '',
isLoading :'false',
error : '',
currentAttribute: '',
attributeList: []
}
function dimensionPickerReducer(state = initialState, action) {
switch(action.type) {
case ATTRIBUTE_SELECTION_CHANGED:
return Object.assign({}, state, {currentAttribute: action.data});
break;
case REQUEST_DIMENSION_ATTRIBUTES:
return Object.assign({}, state, {isLoading: 'true', error: ''})
break;
case DIMENSION_ATTRIBUTES_RECEIVED:
return Object.assign({}, state, {attributeList: action.data, isLoading: 'false', error: action.error});
break;
case SET_DIMENSION_NAME:
return Object.assign({}, state, {dimensionName: action.data})
break;
default:
return state;
break;
}
}
export default dimensionPickerReducer;
我这样建我的 state store
import React from 'react';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import DataTableReducer from './reducers/DataTableReducer';
import DimensionPickerReducer from './reducers/DimensionPickerReducer';
const combinedReducer = combineReducers({
dataTable: DataTableReducer,
dimensionPicker: DimensionPickerReducer
});
export default applyMiddleware(thunk)(createStore)(combinedReducer);
我像这样加载组件
import React from 'react';
import DimensionPicker from '../containers/DimensionPickerContainer';
const App = () => (
<div>
<DimensionPicker dimensionName="Genre"/>
</div>
)
export default App;
最后是我如何加载我的应用程序
import React from 'react';
import {render} from 'react-dom';
import {Provider} from 'react-redux';
import App from './Reports/App.jsx';
import MovieLensAppStore from './stores/MovieLensAppStore';
render (
<Provider store={MovieLensAppStore}>
<App />
</Provider>,
document.getElementById('container')
)
我的期望是
- reducer 将初始化状态
- 容器组件将使用容器组件中的 2 个方法将该状态映射到 props
- 最后,当组件加载时,它将具有可用的状态和调度方法。
但这并没有发生。相反,我收到了
之类的警告
Warning: Failed propType: Required prop `dimensionName` was not specified in `DimensionPicker`. Check the render method of `Connect(DimensionPicker)`.
我在这里发布了我的整个代码库
您将 "initial state" 作为默认参数提供给您的减速器,但这仅在实际调用时用作该减速器的默认状态。由于您尚未发送任何操作,因此初始状态取决于您提供给 createStore
的值,大概在 MovieLensAppStore
.
我不知道您是如何创建商店的,但这应该可行,例如:
createStore(
combineReducers({
dimensionPickerReducer
}),
{
dimensionPicker: {
dimenisionName: '',
isLoading :'false',
error : '',
currentAttribute: '',
attributeList: []
}
}
)
问题已解决。这里麻烦的是 combineReducers 没有正确初始化状态,这就是为什么容器控件说没有指定道具(因为状态为空)。
此处记录了解决方案
我有以下 Redux+React 组件
import {PropTypes, React, Component} from 'react';
import Select from 'react-select';
class DimensionPicker extends Component {
componentDidMount() {
const {onLoad} = this.props;
onLoad();
}
render() {
const {onChange, attributeList, currentAttribute} = this.props;
return (
<div>
<Select value={currentAttribute} options={attributeList} onChange={onChange} />
</div>
)
}
}
DimensionPicker.propTypes = {
dimensionName: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
attributeList: PropTypes.arrayOf(PropTypes.shape({
value: PropTypes.string.isRequired,
label: PropTypes.string.isRequired
}).isRequired).isRequired,
currentAttribute: PropTypes.string.isRequired
}
export default DimensionPicker;
和以下容器组件
import React from 'react';
import DimensionPickerActions from '../actions/DimensionPickerActions';
import {connect} from 'react-redux';
import DimensionPicker from './controls/DimensionPicker.jsx';
const mapStateToProps = (state) => {
return {
dimensionName: state.dimensionName,
attributeList: state.attributeList,
currentAttribute: state.currentAttribute
}
}
const mapDispatchToProps = (state) => {
return {
onChange: (newValue) => {
dispatch(updateAttributeSelection(newValue));
},
onLoad: () => {
dispatch(fetchDimensionAttributes(state.dimensionName));
}
}
}
export default connect(mapStateToProps, mapDispatchToProps)(DimensionPicker);
我还有一个填充初始状态的减速器
// define the state tree for the dimenion picker.
const initialState = {
dimenisionName: '',
isLoading :'false',
error : '',
currentAttribute: '',
attributeList: []
}
function dimensionPickerReducer(state = initialState, action) {
switch(action.type) {
case ATTRIBUTE_SELECTION_CHANGED:
return Object.assign({}, state, {currentAttribute: action.data});
break;
case REQUEST_DIMENSION_ATTRIBUTES:
return Object.assign({}, state, {isLoading: 'true', error: ''})
break;
case DIMENSION_ATTRIBUTES_RECEIVED:
return Object.assign({}, state, {attributeList: action.data, isLoading: 'false', error: action.error});
break;
case SET_DIMENSION_NAME:
return Object.assign({}, state, {dimensionName: action.data})
break;
default:
return state;
break;
}
}
export default dimensionPickerReducer;
我这样建我的 state store
import React from 'react';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import DataTableReducer from './reducers/DataTableReducer';
import DimensionPickerReducer from './reducers/DimensionPickerReducer';
const combinedReducer = combineReducers({
dataTable: DataTableReducer,
dimensionPicker: DimensionPickerReducer
});
export default applyMiddleware(thunk)(createStore)(combinedReducer);
我像这样加载组件
import React from 'react';
import DimensionPicker from '../containers/DimensionPickerContainer';
const App = () => (
<div>
<DimensionPicker dimensionName="Genre"/>
</div>
)
export default App;
最后是我如何加载我的应用程序
import React from 'react';
import {render} from 'react-dom';
import {Provider} from 'react-redux';
import App from './Reports/App.jsx';
import MovieLensAppStore from './stores/MovieLensAppStore';
render (
<Provider store={MovieLensAppStore}>
<App />
</Provider>,
document.getElementById('container')
)
我的期望是
- reducer 将初始化状态
- 容器组件将使用容器组件中的 2 个方法将该状态映射到 props
- 最后,当组件加载时,它将具有可用的状态和调度方法。
但这并没有发生。相反,我收到了
之类的警告Warning: Failed propType: Required prop `dimensionName` was not specified in `DimensionPicker`. Check the render method of `Connect(DimensionPicker)`.
我在这里发布了我的整个代码库
您将 "initial state" 作为默认参数提供给您的减速器,但这仅在实际调用时用作该减速器的默认状态。由于您尚未发送任何操作,因此初始状态取决于您提供给 createStore
的值,大概在 MovieLensAppStore
.
我不知道您是如何创建商店的,但这应该可行,例如:
createStore(
combineReducers({
dimensionPickerReducer
}),
{
dimensionPicker: {
dimenisionName: '',
isLoading :'false',
error : '',
currentAttribute: '',
attributeList: []
}
}
)
问题已解决。这里麻烦的是 combineReducers 没有正确初始化状态,这就是为什么容器控件说没有指定道具(因为状态为空)。
此处记录了解决方案