I'm stuck with the dispatch function where it cannot be read. I want to pull data from store.js file into index.js to display it. I'm currently experimenting Redux for my web project that has huge data.
I'm not really familiar with Redux. Is there any other way to catch a huge data? And what did I go wrong with my code?
//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);