gpt4 book ai didi

reactjs - 使用 Link 导航时,具有 next-redux-wrapper 状态的 Next.js 被重置为初始值

转载 作者:行者123 更新时间:2023-12-05 02:38:06 24 4
gpt4 key购买 nike

Next.js v12.0

next-redux-wrapper.

每当我使用适当的 next/link 元素离开页面然后再次返回(使用另一个 link el)时,状态将重置为初始值,因此会执行另一个提取。奇怪的是,我以相同的方式设置了另一个“事务”切片,除了它包含一组事务对象并且工作正常(导航离开和返回并且数据不会因为它持续存在而重新获取店内)代码低于任何建议,将不胜感激。

商店.js

import { HYDRATE, createWrapper } from "next-redux-wrapper";
import thunkMiddleware from "redux-thunk";
import address from "./address/reducer";
import transactions from "./transaction/reducer";

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({
transactions,
address,
});

const rootReducer = (state, action) => {
if (action.type === HYDRATE) {
const nextState = {
...state, // use previous state
...action.payload, // apply delta from hydration
};

if (state.address.id){
nextState.address = state.address;
}
return nextState;
} else {
return combinedReducer(state, action);
}
};


const initStore = () => {
return createStore(rootReducer, bindMiddleware([thunkMiddleware]));
};
export const wrapper = createWrapper(initStore);

地址/reducer.js


const addressInitialState = {
id: null,
timestamp: null,
address: null,
balance: null,
received: null,
sent: null,
groupid: null,
last_txs: []
};

export default function reducer(state = addressInitialState, action) {
switch (action.type) {
case addressActionTypes.GET_WALLET_DETAILS:
return {id: action.payload.address, ...action.payload};
default:
return state;
}
}

地址/action.js

export const addressActionTypes = {
GET_WALLET_DETAILS: "GET_WALLET_DETAILS",
};

export const getWalletDetails = (address) => {
return async (dispatch) => {
const fetchData = async () => {
const response = await fetch(
`https:someapi.com/api/getaddress/?address=${address}`
);
if (!response.ok) {
throw new Error("Could not fetch address data!");
}

const data = await response.json();
console.log('req sent');
return data;
};
try {
const addressData = await fetchData();
dispatch({
type: addressActionTypes.GET_WALLET_DETAILS,
payload: addressData,
});
} catch (err) {
console.log(err);
}
};
};

页面/[地址].js

import { Fragment } from "react";
import Head from "next/head";
import AddressDetails from "../../../components/crypto/rvn/AddressDetails";
import AddressTransactions from "../../../components/crypto/rvn/AddressTransactions";
import { connect } from "react-redux";
import { getWalletDetails } from "../../../store/address/action";
import { wrapper } from "../../../store/store";

function Address(props) {
return (
<Fragment>
<Head>
<title>RVN</title>
<meta name="description" content="RVN Address" />
</Head>
<AddressDetails address={props.addressDetails}></AddressDetails>
<AddressTransactions
transactions={props.addressDetails["last_txs"]}
address={props.addressDetails.address}
></AddressTransactions>
</Fragment>
);
}

export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (context) => {
const state = store.getState();
if(state.address.id === null) {
await store.dispatch(getWalletDetails(context.params.address));
}
else{
return{
props: {
addressDetails: state.address,
}
}
}
}
);


const mapStateToProps = (state) => ({
addressDetails: state.address,
});
export default connect(mapStateToProps, null)(Address);

最佳答案

通过转换这个解决了这个问题

const initStore = () => {
return createStore(rootReducer, bindMiddleware([thunkMiddleware]));
};

在 store.js 中哪个直接来自https://github.com/vercel/next.js/tree/canary/examples/with-redux-thunk

对此

const store = createStore(rootReducer, bindMiddleware([thunkMiddleware]));

const initStore = () => {
return store
};

所以它不会在每次使用包装器时都重新初始化存储

这更符合文档在 https://github.com/kirill-konshin/next-redux-wrapper

关于reactjs - 使用 Link 导航时,具有 next-redux-wrapper 状态的 Next.js 被重置为初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69781714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com