ERROR: Using UNSAFE_componentWillReceiveProps (NextJs / Redux)
ERROR: Using UNSAFE_componentWillReceiveProps (NextJs / Redux)
今天我开始构建一个 Next.js 应用程序。
我正在使用 Redux (next-redux-wrapper
) 来管理我的全局状态。
(我也用 redux-persist 坚持一些减速器)。
我刚刚实现 redux-thunk
并立即得到一个非常奇怪的错误。我真的不知道为什么会出现该错误。我基本上只是做了设置,没有创建任何减速器(在我创建减速器之后错误仍然存在)。
这不是第一次了,我做了同样的设置没有任何问题,但那次我无法摆脱那个错误。
错误代码
Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not
recommended and may indicate bugs in your code. See
https://reactjs.org/link/unsafe-component-lifecycles for details.***
- Move data fetching code or side effects to componentDidUpdate.
- If you're updating state whenever props change, refactor your code to use memoization ***techniques or move it to static
getDerivedStateFromProps. Learn more at:
https://reactjs.org/link/derived-state Please update the following
components: withRedux(MyApp)
如果您需要更多代码,请询问。我真的没有更多了。也许 package.json
.
_app.js
import App from 'next/app'
import { wrapper } from '../reduxStore/store'
import { useStore } from 'react-redux'
import PropTypes from 'prop-types'
function MyApp({ Component, pageProps }) {
const store = useStore((state) => state)
return <Component {...pageProps} />
}
MyApp.propTypes = {
Component: PropTypes.func,
pageProps: PropTypes.object,
}
export default wrapper.withRedux(MyApp)
store.js
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { HYDRATE, createWrapper } from 'next-redux-wrapper'
import thunkMiddleware from 'redux-thunk'
import userReducer from './reducers/userReducer'
const bindMiddleware = (middleware) => {
if (process.env.NODE_ENV !== 'production') {
const { composeWithDevTools } = require('redux-devtools-extension')
return composeWithDevTools(applyMiddleware(...middleware))
}
return applyMiddleware(...middleware)
}
const combinedReducer = combineReducers({
user: userReducer,
})
const reducer = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state, // use previous state
...action.payload, // apply delta from hydration
}
// if (state.count.count) nextState.count.count = state.count.count // preserve count value on client side navigation
return nextState
} else {
return combinedReducer(state, action)
}
}
export const makeStore = ({ isServer }) => {
if (isServer) {
//If it's on server side, create a store
return createStore(reducer, bindMiddleware([thunkMiddleware]))
} else {
//If it's on client side, create a store which will persist
const { persistStore, persistReducer } = require('redux-persist')
const storage = require('redux-persist/lib/storage').default
const persistConfig = {
key: 'nextjs',
whitelist: ['user'], // only counter will be persisted, add other reducers if needed
storage, // if needed, use a safer storage
}
const persistedReducer = persistReducer(persistConfig, reducer) // Create a new reducer with our existing reducer
const store = createStore(
persistedReducer,
bindMiddleware([thunkMiddleware]),
) // Creating the store again
store.__persistor = persistStore(store) // This creates a persistor object & push that persisted object to .__persistor, so that we can avail the persistability feature
return store
}
}
export const wrapper = createWrapper(makeStore)
这是因为第三方库正在使用 componentWillReceiveProps
- componentWillReceiveProps
自动重命名为 UNSAFE_componentWillReceiveProps
。发生这种情况是因为这些方法现在被视为遗留代码,React is deprecating 该生命周期方法以及其他方法。
不幸的是,他们不是立竿见影的解决方案。
- 您可以分叉代码并自行更新
- 在代码的 GIT 页面上提出问题,希望他们的维护者更新后能解决问题。
- 找另一个图书馆做同样的工作。
- 编写自定义逻辑来完成与库相同的事情
- 使用该库并希望他们在它被 React 弃用之前修复它。
第三方库(next-redux-wrapper)导致的关于Legacy Lifecycle Methods(UNSAFE)的错误,之前的名称是componentWillReceiveProps,该名称将一直有效到版本17。
修复它
您可以使用 react-codemod
自动更新您的组件。并使用重命名不安全生命周期 属性
npx react-codemod rename-unsafe-lifecycles <path>
我的第三方库路径是
路径=./node_module/next-redux-wrapper/lib/index.js
谢谢
今天我开始构建一个 Next.js 应用程序。
我正在使用 Redux (next-redux-wrapper
) 来管理我的全局状态。
(我也用 redux-persist 坚持一些减速器)。
我刚刚实现 redux-thunk
并立即得到一个非常奇怪的错误。我真的不知道为什么会出现该错误。我基本上只是做了设置,没有创建任何减速器(在我创建减速器之后错误仍然存在)。
这不是第一次了,我做了同样的设置没有任何问题,但那次我无法摆脱那个错误。
错误代码
Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code. See https://reactjs.org/link/unsafe-component-lifecycles for details.***
- Move data fetching code or side effects to componentDidUpdate.
- If you're updating state whenever props change, refactor your code to use memoization ***techniques or move it to static getDerivedStateFromProps. Learn more at: https://reactjs.org/link/derived-state Please update the following components: withRedux(MyApp)
如果您需要更多代码,请询问。我真的没有更多了。也许 package.json
.
_app.js
import App from 'next/app'
import { wrapper } from '../reduxStore/store'
import { useStore } from 'react-redux'
import PropTypes from 'prop-types'
function MyApp({ Component, pageProps }) {
const store = useStore((state) => state)
return <Component {...pageProps} />
}
MyApp.propTypes = {
Component: PropTypes.func,
pageProps: PropTypes.object,
}
export default wrapper.withRedux(MyApp)
store.js
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { HYDRATE, createWrapper } from 'next-redux-wrapper'
import thunkMiddleware from 'redux-thunk'
import userReducer from './reducers/userReducer'
const bindMiddleware = (middleware) => {
if (process.env.NODE_ENV !== 'production') {
const { composeWithDevTools } = require('redux-devtools-extension')
return composeWithDevTools(applyMiddleware(...middleware))
}
return applyMiddleware(...middleware)
}
const combinedReducer = combineReducers({
user: userReducer,
})
const reducer = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state, // use previous state
...action.payload, // apply delta from hydration
}
// if (state.count.count) nextState.count.count = state.count.count // preserve count value on client side navigation
return nextState
} else {
return combinedReducer(state, action)
}
}
export const makeStore = ({ isServer }) => {
if (isServer) {
//If it's on server side, create a store
return createStore(reducer, bindMiddleware([thunkMiddleware]))
} else {
//If it's on client side, create a store which will persist
const { persistStore, persistReducer } = require('redux-persist')
const storage = require('redux-persist/lib/storage').default
const persistConfig = {
key: 'nextjs',
whitelist: ['user'], // only counter will be persisted, add other reducers if needed
storage, // if needed, use a safer storage
}
const persistedReducer = persistReducer(persistConfig, reducer) // Create a new reducer with our existing reducer
const store = createStore(
persistedReducer,
bindMiddleware([thunkMiddleware]),
) // Creating the store again
store.__persistor = persistStore(store) // This creates a persistor object & push that persisted object to .__persistor, so that we can avail the persistability feature
return store
}
}
export const wrapper = createWrapper(makeStore)
这是因为第三方库正在使用 componentWillReceiveProps
- componentWillReceiveProps
自动重命名为 UNSAFE_componentWillReceiveProps
。发生这种情况是因为这些方法现在被视为遗留代码,React is deprecating 该生命周期方法以及其他方法。
不幸的是,他们不是立竿见影的解决方案。
- 您可以分叉代码并自行更新
- 在代码的 GIT 页面上提出问题,希望他们的维护者更新后能解决问题。
- 找另一个图书馆做同样的工作。
- 编写自定义逻辑来完成与库相同的事情
- 使用该库并希望他们在它被 React 弃用之前修复它。
第三方库(next-redux-wrapper)导致的关于Legacy Lifecycle Methods(UNSAFE)的错误,之前的名称是componentWillReceiveProps,该名称将一直有效到版本17。
修复它
您可以使用 react-codemod
自动更新您的组件。并使用重命名不安全生命周期 属性
npx react-codemod rename-unsafe-lifecycles <path>
我的第三方库路径是
路径=./node_module/next-redux-wrapper/lib/index.js
谢谢