bier hier
bier hier

Reputation: 22580

How to get reducer working?

I just started learning reactjs&redux and trying to implement adding items to a list. This is my rootcomponent:

//create store based on reducer
const store = createStore(listItem)
const rootEl = document.getElementById('root')

//render root component
const render = () => ReactDOM.render(
  <List
        addToList={() => store.dispatch({ type: 'ADD_TO_LISTS' },'testing')}
  />,
  rootEl
)

//call
render()

//subscribe the store
store.subscribe(render)

In the list component the render method looks like this:

 render() {
    const { addToList } = this.props
    return (
      <p>
        <input type="text"  />
        <button onClick={addToList}>add</button>
       </p>
    )
  }

The reducer looks like this:

const listItem = (state, action)  => {
  switch (action.type) {
    case 'ADD_TO_LIST':
      return [...state,action.text]

    default:
      return state
  }
}

However when I run the app it throws an error:

Uncaught error: expected reducer to be a function

What is the issue with my code?

Upvotes: 0

Views: 57

Answers (1)

bier hier
bier hier

Reputation: 22580

I figured it out, forgot to put 'export default listItem' at the end of the reducer

Upvotes: 1

Related Questions