Ned*_* DN 33 javascript reactjs redux react-proptypes
我正在使用redux但是当我运行我的代码时出现此错误:
不推荐通过主React包访问PropTypes.请改用npm中的prop-types包.
我安装
npm i prop-types -S
但我仍然有同样的错误.
./components/action/article.js
import * as ArticleActionTypes from '../actiontypes/article';
export const AddArticle = (name, description, prix, image) => {
return {
type: ArticleActionTypes.ADD_ARTICLE,
name,
description,
prix,
image
}
}
export const RemoveArticle = index => {
return {
type: ArticleActionTypes.REMOVE_ARTICLE,
index
}
}
Run Code Online (Sandbox Code Playgroud)
./components/actiontypes/article.js
export const ADD_ARTICLE = 'article/ADD_ARTICLE';
export const REMOVE_ARTICLE = 'article/REMOVE_ARTICLE';
export const UPDATE_ARTICLE = 'article/UPDATE_ARTICLE';
Run Code Online (Sandbox Code Playgroud)
./components/reducers/article.js
import * as ArticleActionTypes from '../actiontypes/article';
const initialState = [
{
name: 'test',
description: 'test',
prix: 'test',
image: 'url'
},
{
name: 'test',
description: 'test',
prix: test,
image: 'url'
}
]
export default function Article (state=initialState, action){
switch(action.type){
case ArticleActionTypes.ADD_ARTICLE :
return [
...state,
{
name: action.name,
description: action.description,
prix: action.prix,
image: action.image
}
];
case ArticleActionTypes.REMOVE_ARTICLE :
return [
...state.slice(0, action.index),
...state.slice(action.index +1)
] ;
default: return state;
}
}
Run Code Online (Sandbox Code Playgroud)
index.js
import React from 'react';
import { render } from 'react-dom';
import {Provider} from 'react-redux';
import {createStore} from 'redux';
import ArticleReducer from './components/reducers/article';
import Scoreboard from './components/containers/Scoreboard';
const store = createStore(
ArticleReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
render(<Provider>
<Scoreboard store={store}/>
</Provider>, document.getElementById('root'));
Run Code Online (Sandbox Code Playgroud)
./components/containers/Scorboard.js
import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreactors} from 'redux';
import PropTypes from 'prop-types';
class Scoreboard extends React.Component {
render(){
return (
<div>
Scoreboard
</div>
)
}
}
const mapStateToProps = state => {
{
articles :state
}
}
Scoreboard.propTypes = {
articles: PropTypes.array.isRequired
}
export default connect(mapStateToProps)(Scoreboard);
Run Code Online (Sandbox Code Playgroud)
Yuv*_*val 16
由于React v15.5.0 React.PropTypes的发布已弃用并已移至另一个库.您应该使用npm install prop-types并使用PropTypes.
./components/containers/Scorboard.js中的代码看起来非常好,您可能React.PropTypes在代码中的其他位置.
另一个选择是你正在使用的某些依赖仍然是旧的方式.您可以尝试更新您的依赖项,但由于此弃用很新,我怀疑每个库都已发布更新.
有关PropTypes弃用的更多详细信息,请单击此处.
UPDATE
似乎redux已经更新了它的依赖关系以使用React v15.5.0并且摆脱了弃用警告.它也在v4和v5中修复.
我这样解决了这个警告:
已安装的PropTypes
# npm install -S prop-types
像这样导入PropTypes
import PropTypes from 'prop-types';
而不是这样做:
import { PropTypes } from 'react';
| 归档时间: |
|
| 查看次数: |
20941 次 |
| 最近记录: |