我正在使用 redux,但是当我运行我的代码时出现这个错误:
Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.
我安装
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
}
}
./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';
./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;
}
}
索引.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'));
./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);
最佳答案
正如其他人所提到的 - 如果您已经针对 PropTypes
使用审核了自己的项目,但您仍然看到警告 - 它可能来自上游依赖项。追踪此警告原因的一种方法是在记录它的位置设置调试断点,然后返回到调用方。这是我制作的一个简单的录音示例:
(提供更高质量的版本 here 。)
一旦您确定了有问题的库,请考虑提交一个 Github 问题(或者更好的是 - 一个 PR!)以通知作者新警告。
与此同时,这只是一个开发模式警告,因此它不会影响应用程序的生产使用。
关于javascript - 通过主 React 包访问 PropTypes 已被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303761/