【发布时间】:2021-09-20 11:05:58
【问题描述】:
我被无法读取的调度功能所困扰。我想将 store.js 文件中的数据提取到 index.js 中以显示它。我目前正在为我的拥有大量数据的 Web 项目试验 Redux。
我对 Redux 不是很熟悉。有没有其他方法可以捕获大量数据?我的代码出了什么问题?
//This is the store.js file
import { createWrapper } from 'next-redux-wrapper';
import { createStore } from 'redux';
import data from './pages/API/data.json';
//initial state
const startState = {
cards: []
};
//Actions
export const initialCards = () => {
return {
type: 'INITIALCARDS',
cards: data
}
};
export const addItem = (item) => {
return {
type: 'ADD',
item
}
};
//create a reducer
export const reducer = (state = initialState, action) => {
switch(action.type){
case 'INITIALCARDS':
return {
cards: action.cards,
}
case 'ADD':
return {
...state,
cards: [...state.cards, action.item],
}
default: return state
}
}
//create store
const store = (initialState = startState) => {
return createStore(reducer, initialState);
};
export const initStore = createWrapper(store);
//This is the index.js file
import React from 'react';
import styles from './index.module.css';
import Card from './Card';
import { initStore, initialCards, addItem } from '../store';
class Index extends React.Component {
static async getInitialProps ({ store }){
return store.dispatch(initialCards());
}
render (){
return (
<div className={styles.app}>
<header className={styles.header}>
<img src="/logo.png" className={styles.logo} alt="logo" />
</header>
<div className={styles.grid}>
{
this.props.cards.map((card) => (
<Card key={card.id} />
))
}
</div>
{/*<button onClick={() => dispatch(addItem(item))}></button>*/}
</div>
)
}
};
export default initStore.withRedux(Index);
【问题讨论】:
-
@Luka 还没有,我现在去看看。谢谢
标签: reactjs react-redux next.js