redux

NaN problem with react-redux and react-thunk

余生颓废 提交于 2020-03-23 12:01:38
问题 I'm trying to get data from api about currencies and I get error about NaN. I console.log(this.props) There it is: There is what I already did. In my actionTypes.js export const FETCH_DATA_BEGIN = 'FETCH_DATA_BEGIN'; export const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS'; export const FETCH_DATA_FAIL = 'FETCH_DATA_FAIL'; and also there is my action.js export const fetchData = () => { return dispatch => { fetch(`https://api.exchangeratesapi.io/latest?base=${this.props.base}`) .then(res => res

redux 基础

我是研究僧i 提交于 2020-03-23 09:42:53
什么时候用? 某个组件的状态,需要共享 某个状态需要在任何地方都可以拿到 一个组件需要改变全局状态 一个组件需要改变另一个组件的状态 你用react不能解决的组件通信,redux可以给你解决。 redux核心概念 store相当于一个应用的state,但是不可随意更改,必须通过action更改。 强制使用 action 来描述所有变化带来的好处是可以清晰地知道应用中到底发生了什么。 为了把 action 和 state 串起来,开发一些函数,这就是 reducer。 reducer 只是一个接收 state 和 action,并返回新的 state 的函数。 这差不多就是 Redux 思想的全部。 redux流程 1.用户触发action store.dispatch(action); 2.Store 自动调用 Reducer(Store 收到 Action 以后,必须给出一个新的 State,这样 View 才会发生变化。这种 State 的计算过程就叫做 Reducer。),并且传入两个参数:当前 State 和收到的 Action。 Reducer 会返回新的 State 。 3.State 一旦有变化,Store 就会调用监听函数 store.subscribe(listener); listener可以通过store.getState()得到当前状态。 PS

React Redux fetching data from backend approach

主宰稳场 提交于 2020-03-23 08:00:36
问题 I'm a bit confused and would love an answer that will help me to clear my thoughts. Let's say I have a backend (nodejs, express etc..) where I store my users and their data, and sometimes I wanna fetch data from the backend, such as the user info after he logs in, or a list of products and save them in the state. My approach so far and what I've seen, I fetch the data before the component loads and dispatch an action with the data from the response. But I recently started digging a bit about

Redux connect function explanation

廉价感情. 提交于 2020-03-23 07:52:09
问题 I am new to redux framework. What is App means in the below code. Is it passing Type, Class to connect function? I would like to know how does it work. Apparently connect function returns a new module for export. Can someone point to basic javascript example to understand this code. export default connect(mapDispatchToProps)(App); 回答1: App is the current component name, if your component name is Mycomp then you can bind with Mycomp if it's a class component. If it is a functional component

学习react基础知识(五)

和自甴很熟 提交于 2020-03-22 20:42:44
redux 全局状态管理 react-redux 优化模块 优化redux的使用过程 npm install redux react-redux 通过react-redux 提供的provider提供器 将全局状态对象挂载到根元素的上下文上 import Provider from 'react-redux'import store from './store/store.js'...<Provider store = {store}> <App></App></Provider>... 在组件中使用全局状态值 通过react-redux 提供的connect 从跟组件的上下文上获取store对象, 通过高阶组件将获得到的store传给目标组件的props import {connect} from 'react-redux' class Son1 ... {} connect(state=>state)(Son1) 组件中修改全局状态值 不需要写组件监听 组件 -> actionCreator -> redcuer ->组件(监听更新) 组件的里的监听不用写了 actionCreator 默认做两个事情1.创建action 2.通过dispath 发送action actionCreator 使用插件之后只要 创建action 在组件里发送 react-redux

react + react-router + redux + ant-Desgin 搭建react管理后台 -- 引入ant-Desgin及布局(三)

笑着哭i 提交于 2020-03-22 15:00:58
前言    学习总结使用,博客如中有错误的地方,请指正。改系列文章主要记录了搭建一个管后台的步骤,主要实现的功能有:使用路由模拟登录、退出、以及切换不同的页面;使用redux实现面包屑;引入使用其他常用的组件,比如highchart、富文本等,后续会继续完善。    github地址: https://github.com/huangtao5921/react-antDesgin-admin (欢迎Star)   项目展示地址: https://huangtao5921.github.io/react-admin/ 一、认识目录文件    上一篇博客 react + react-router + redux + ant-Desgin 搭建react管理后台 -- 初始化项目(一) 已经初始化了一个基本的项目,接下来认识一下整个项目目录结构: ├── config // webpack配置,里面还包含其他的文件 ├── node_modules // 项目依赖包文件夹 ├── piblic │ ├── favicon.ico // 浏览器icon │ ├── index.html // 单页面入口文件 │ ├── manifest.json ├── src │ ├── App.css // 可删除 │ ├── App.js │ ├── App.test.js // 可删除 │ ├──

Action does not trigger a reducer in React + Redux

感情迁移 提交于 2020-03-21 16:07:58
问题 I'm working on a react-redux app and for some reason the action I call does not reach the reducer (in which I currently only have a log statement). I have attached the code I feel is relevant and any contributions would be highly appreciated. Action called within function in component: onSearchPressed() { console.log('search pressed'); this.props.addToSaved(); } actions/index.js: var actions = exports = module.exports exports.ADD_SAVED = "ADD_SAVED"; exports.addToSaved = function addToSaved()

Action does not trigger a reducer in React + Redux

三世轮回 提交于 2020-03-21 16:07:28
问题 I'm working on a react-redux app and for some reason the action I call does not reach the reducer (in which I currently only have a log statement). I have attached the code I feel is relevant and any contributions would be highly appreciated. Action called within function in component: onSearchPressed() { console.log('search pressed'); this.props.addToSaved(); } actions/index.js: var actions = exports = module.exports exports.ADD_SAVED = "ADD_SAVED"; exports.addToSaved = function addToSaved()

How to use redux-thunk from node server side?

微笑、不失礼 提交于 2020-03-21 07:00:18
问题 (code from https://redux.js.org/advanced/async-actions) The code setups a redux store and then calls dispatch on the store with some actions. The store use redux-thunk to manage async API calls. Here is the index.js import reduxThunk from 'redux-thunk' const { thunkMiddleware } = reduxThunk; import redux from 'redux'; const { createStore } = redux; const { applyMiddleware } = redux; import { selectSubreddit, fetchPosts } from './actions.js' import rootReducer from './reducers.js' const store

Reactjs and redux - How to prevent excessive api calls from a live-search component?

别来无恙 提交于 2020-03-21 05:13:52
问题 I have created this live-search component: class SearchEngine extends Component { constructor (props) { super(props); this.handleChange = this.handleChange.bind(this); this.handleSearch = this.handleSearch.bind(this); } handleChange (e) { this.props.handleInput(e.target.value); //Redux } handleSearch (input, token) { this.props.handleSearch(input, token) //Redux }; componentWillUpdate(nextProps) { if(this.props.input !== nextProps.input){ this.handleSearch(nextProps.input, this.props