Ad

Sending Actions In Redux, If There Are Several Cases In The Reducer Instruction In Switch

- 1 answer

In actions, I declared the actionexport const CLEAR_ARRAY_TODOS = 'CLEAR_ARRAY_TODOS'; I imported this action in reducers. And I created a new case case 'CLEAR_ARRAY_TODOS' in reducers` in the switch statement:     

 case 'CLEAR_ARRAY_TODOS':
  return {
    todos: [],
  };

In thetodos component I imported the action CLEAR_ARRAY_TODOS. And here I have a problem as in mapDispatchToProps in the functiongetTodos send this action CLEAR_ARRAY_TODOS and connect to the buttonClear Array Todos?

Demo here: https://stackblitz.com/edit/react-iuvdna?file=reducers%2Findex.js

Actions

import axios from 'axios';

export const GET_TODOS = 'GET_TODOS';
export const CLEAR_ARRAY_TODOS = 'CLEAR_ARRAY_TODOS';
export const FETCH_SUCCESS = 'FETCH_SUCCESS';
export const FETCH_FAILURE = 'FETCH_FAILURE';

export const getTodos = () => 
dispatch => {

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
    })
    .then(({data})=> {
      console.log(data);

      dispatch({type: GET_TODOS, payload:{
        data 
      }});   
    })
    .catch(error => {
      console.log(error);

      dispatch({type: FETCH_FAILURE})
    });
};

export const getTodo = () => 
dispatch => {

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
    })
    .then(({data})=> {
      console.log(data);

      dispatch({type: GET_TODOS, payload:{
        data 
      }});   
    })
    .catch(error => {
      console.log(error);

      dispatch({type: FETCH_FAILURE})
    });
};

Reducers

import {GET_TODOS, CLEAR_ARRAY_TODOS} from '../../actions';

const initialState = {
  todos: []
};

const rootReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'GET_TODOS':
      return {
        ...state,
        todos: action.payload.data,
        todo: action.payload.data[0]
      };
     case 'CLEAR_ARRAY_TODOS':
      return {
        todos: [],
      };
    default:
      return state;
  }
};

export default rootReducer;

Todos

import React, { Component } from 'react';
import { connect } from 'react-redux';
import {getTodos, CLEAR_ARRAY_TODOS} from '../.././actions';

class Todos extends Component {
  componentDidMount() {
    this.props.getTodos(); 
  }

  render() {
    return (
      <div>
        <button>Clear array Todos</button>
        <ul>
          {this.props.todos.map(todo => {
          return <li key={todo.id}>
                    {todo.title}
                </li>
          })}
        </ul>
      </div>
    );
  }
}

const mapStateToProps = state => {
  console.log(state.todos);
  console.log(state.todo);
  const { todos } = state;

  return {
    todos
  };
};

const mapDispatchToProps = dispatch => ({
  getTodos: () => dispatch(getTodos())
});

export default connect(mapStateToProps, mapDispatchToProps)(Todos);
Ad

Answer

CLEAR_ARRAY_TODOS is not an action, is just a variable holding an action type string. You should add a clearTodos action

export const clearTodos = { type: CLEAR_ARRAY_TODOS }

or action creator

export const clearTodos = () => ({ type: CLEAR_ARRAY_TODOS })

and use that in your component mapDispatchToProps (like you do with getTodos)

import React, { Component } from 'react';
import { connect } from 'react-redux';
import {getTodos, clearTodos} from '../.././actions';

class Todos extends Component {
  componentDidMount() {
    this.props.getTodos(); 
  }

  render() {
    return (
      <div>
        <button onClick={ this.props.clearTodos }>Clear array Todos</button>
        <ul>
          {this.props.todos.map(todo => {
          return <li key={todo.id}>
                    {todo.title}
                </li>
          })}
        </ul>
      </div>
    );
  }
}

const mapStateToProps = state => {
  console.log(state.todos);
  console.log(state.todo);
  const { todos } = state;

  return {
    todos
  };
};


const mapDispatchToProps = dispatch => ({
  getTodos: () => dispatch(getTodos()),
  clearTodos: () => dispatch(clearTodos())
});

export default connect(mapStateToProps, mapDispatchToProps)(Todos);
Ad
source: stackoverflow.com
Ad