Javascript 在react redux中接收数据后在另一个函数内调用函数

Javascript 在react redux中接收数据后在另一个函数内调用函数,javascript,reactjs,redux,Javascript,Reactjs,Redux,如果我收到数据,如何在gettoos函数中调用getTodo函数如果(数据){getTodo()}?单击按钮的预期效果->调用函数gettoos->if(data){getTodo()} 我试图在操作中调用getTodo(): .then (({data}) => { dispatch ({type: GET_TODOS, payload: date }); }, () => getTodo ()) 此处演示: 行动 impo

如果我收到数据,如何在
gettoos
函数中调用
getTodo
函数<代码>如果(数据){getTodo()}?
单击按钮的预期效果
->调用函数
gettoos
->
if(data){getTodo()}

我试图在操作中调用
getTodo()

.then (({data}) => {
       dispatch ({type: GET_TODOS, payload:
         date
       });
     }, () => getTodo ())
此处演示:

行动

import axios from 'axios';

export const GET_TODOS = 'GET_TODOS';
export const GET_TODO = 'GET_TODO';
export const FETCH_FAILURE = 'FETCH_FAILURE';

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODOS, payload:
        data 
      });   
    },() => getTodo())
    .catch(error => {
      console.log(error);

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

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos/1',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODO, payload:
        data 
      });   
    })
    .catch(error => {
      console.log(error);

      dispatch({type: FETCH_FAILURE})
    });
};
import {GET_TODOS, GET_TODO} from '../../actions';

import { combineReducers } from 'redux';

const todos = (state = [], action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODOS':
      return payload;
    default:
      return state;
  }
};

const todo = (state = {}, action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODO':
      return payload;
    default:
      return state;
  }
};


const rootReducer = combineReducers({
  todos, 
  todo
});

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

class Todos extends Component {
  constructor(props){
    super(props);
  }
  render() {
    return (
      <div>
        <button onClick={this.props.getTodos}>Get Todos</button>
        <ul>
          {this.props.todos.map(todo => {
          return <li key={todo.id}>
                    {todo.title}
                </li>
          })}
        </ul>
        {this.props.todo.id}
      </div>
    );
  }
}

const mapStateToProps = state => {
  const { todos, todo } = state;
  return {
    todos, 
    todo
  };
};

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

export default connect(mapStateToProps, mapDispatchToProps)(Todos);
减速器

import axios from 'axios';

export const GET_TODOS = 'GET_TODOS';
export const GET_TODO = 'GET_TODO';
export const FETCH_FAILURE = 'FETCH_FAILURE';

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODOS, payload:
        data 
      });   
    },() => getTodo())
    .catch(error => {
      console.log(error);

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

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos/1',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODO, payload:
        data 
      });   
    })
    .catch(error => {
      console.log(error);

      dispatch({type: FETCH_FAILURE})
    });
};
import {GET_TODOS, GET_TODO} from '../../actions';

import { combineReducers } from 'redux';

const todos = (state = [], action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODOS':
      return payload;
    default:
      return state;
  }
};

const todo = (state = {}, action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODO':
      return payload;
    default:
      return state;
  }
};


const rootReducer = combineReducers({
  todos, 
  todo
});

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

class Todos extends Component {
  constructor(props){
    super(props);
  }
  render() {
    return (
      <div>
        <button onClick={this.props.getTodos}>Get Todos</button>
        <ul>
          {this.props.todos.map(todo => {
          return <li key={todo.id}>
                    {todo.title}
                </li>
          })}
        </ul>
        {this.props.todo.id}
      </div>
    );
  }
}

const mapStateToProps = state => {
  const { todos, todo } = state;
  return {
    todos, 
    todo
  };
};

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

export default connect(mapStateToProps, mapDispatchToProps)(Todos);
待办事项

import axios from 'axios';

export const GET_TODOS = 'GET_TODOS';
export const GET_TODO = 'GET_TODO';
export const FETCH_FAILURE = 'FETCH_FAILURE';

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODOS, payload:
        data 
      });   
    },() => getTodo())
    .catch(error => {
      console.log(error);

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

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

  return axios({
      url: 'https://jsonplaceholder.typicode.com/todos/1',
      method: 'GET',
    })
    .then(({data})=> {
      dispatch({type: GET_TODO, payload:
        data 
      });   
    })
    .catch(error => {
      console.log(error);

      dispatch({type: FETCH_FAILURE})
    });
};
import {GET_TODOS, GET_TODO} from '../../actions';

import { combineReducers } from 'redux';

const todos = (state = [], action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODOS':
      return payload;
    default:
      return state;
  }
};

const todo = (state = {}, action) => {
  const { type, payload } = action;

  switch (action.type) {
    case 'GET_TODO':
      return payload;
    default:
      return state;
  }
};


const rootReducer = combineReducers({
  todos, 
  todo
});

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

class Todos extends Component {
  constructor(props){
    super(props);
  }
  render() {
    return (
      <div>
        <button onClick={this.props.getTodos}>Get Todos</button>
        <ul>
          {this.props.todos.map(todo => {
          return <li key={todo.id}>
                    {todo.title}
                </li>
          })}
        </ul>
        {this.props.todo.id}
      </div>
    );
  }
}

const mapStateToProps = state => {
  const { todos, todo } = state;
  return {
    todos, 
    todo
  };
};

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

export default connect(mapStateToProps, mapDispatchToProps)(Todos);
import React,{Component}来自'React';
从'react redux'导入{connect};
从“../.././actions”导入{gettoos,clearTodos,getTodo};
类Todos扩展组件{
建造师(道具){
超级(道具);
}
render(){
返回(
抓紧时间
    {this.props.todos.map(todo=>{ return
  • {todo.title}
  • })}
{this.props.todo.id} ); } } 常量mapStateToProps=状态=>{ const{todos,todo}=状态; 返回{ 待办事项, 待办事项 }; }; const mapDispatchToProps=调度=>({ getTodos:()=>dispatch(getTodos()), getTodo:()=>dispatch(getTodo()) }); 导出默认连接(mapStateToProps、mapDispatchToProps)(Todos);