redux

Prevent componentDidMount from fetching data if already available from server-side

有些话、适合烂在心里 提交于 2019-12-14 01:46:18
问题 ComponentDidMount() is triggered when the component is mounted, including when it is hydrated following server-side rendering. One of the solutions I found online is checking whether we have data in the state; however this requires a lot of code to include on every component. What are other solutions? componentDidMount() { // if rendered initially, we already have data from the server // but when navigated to in the client, we need to fetch if (!this.state.data) { this.constructor.fetchData

Testing React Redux - cannot read properties of undefined, or wrapper undefined

佐手、 提交于 2019-12-14 01:30:03
问题 Im having a bit of a problem setting up a Redux store in my component for my test suite. The problem is that even if I try a unconnected mount then the test throws errors looking for variables in authState. I have the following component: import React, { Component } from 'react'; import { Link, Redirect } from "react-router-dom"; import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import * as authActions from '../../../actions/auth.actions'; export class

Timeout feature in the axios library is not working

廉价感情. 提交于 2019-12-14 00:28:39
问题 I have set axios.defaults.timeout = 1000; I stopped the server that provides me with the APIs. But it takes more than 1s to timeout after sending a request. This is how my request looks: import axios from 'axios'; axios.defaults.timeout = 1000; return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => { console.log(response); if(response.status === 200) { // If login was successful, set the token in local storage localStorage.setItem(`${role}_log_toks`, JSON.stringify(response

使用dva改造旧项目的数据流方案

☆樱花仙子☆ 提交于 2019-12-13 20:42:21
前言 最近在给自己的脚手架项目转到TypeScript时,遇到了一些麻烦。 项目之前采用的是react + react-redux + redux-thunk + redux-actions +redux-promise的体系。 当项目转TypeScript时,react和react-redux这种完美转换。 redux-actions转换也初步完成,但是各种为了适应TypeScript的声明很奇怪, 并且有些类型推断错误。 百度了一下,用typesafe-actions去替换,然后与redux-thunk 和redux-promise结合后,各种TypeScript类型错误。 这些类型推断稀奇古怪,网上也没有这个技术体系的相关文章,下班后调试代码,内心逐渐崩溃。 于是有了接下来的举措,用dva去替换react-redux + redux-thunk + redux-actions +redux-promise的数据流方案。 关于dva 关于dva的介绍咱们就不说了,这里给个链接: dva指南 。 明白它是基于 redux + react-router + redux-saga 的封装就够了。 我之前的脚手架是将action,reducer+initialState 分成了不同的文件来处理。 而dva将 reducer, initialState, action, saga

Update the redux state in the reducer having array of objects

醉酒当歌 提交于 2019-12-13 20:19:09
问题 I am new to the react-redux. I do have an object which is like, const initialState = { Low: [ { id: 0, technologyId: 0, technology: '', level: 'EASY' } ], Medium: [ { id: 0, technologyId: 0, technology: '', level: 'MEDIUM' } ], High: [ { id: 0, technologyId: 0, technology: '', level: 'TOUGH' } ] } Now, export default function QuizData(state = initialState, action) { switch (action.type) { case QUIZ_DATA: return { ...state, Low: action.data, error: false, } case RESET_SETUP_QUIZ: { console.log

How do you update some state with a redux get function? Values not updating correctly on set

谁都会走 提交于 2019-12-13 18:16:50
问题 I currently am fetching some balances via a redux getBalances method. When the app initializes it sets 'balances' to the JSON of information, however when I call getBalances again it doesn't re-set the balances (no idea why). So right now, I'm manually trying to update the balances by calling the getBalances method then setting the result of that to balances, however I'm running into walls. All I'd like to do is getBalances again and merely set this to balances, however I'm not sure how I'd

React/Redux Component will not render

余生长醉 提交于 2019-12-13 17:33:09
问题 My component is not rendering and I am not getting any errors. I have been stuck on this error for a few hours now so I am looking for any advice! I am trying to get a data to display when page loads using componentWillMount() and nothing is currently showing up. Before I was able to render simple strings with the component. Now I am not getting any console logs from components, not text, nothing... my api works but I am not getting http calls in chrome console. Below are my files.

Redux: Reusable actions

纵然是瞬间 提交于 2019-12-13 17:31:01
问题 I have an action, that uses a redux thunk, that looks like so: export function fetchData(query) { return dispatch => { return fetch(`http://myapi?query=${query}` ,{mode: 'cors'}) .then(response => response.json()) .then(json => { dispatch(someOtherAction(json)) }) } } } and then my someOtherAction actually updates state: export function someOtherAction(data) { return { action: types.SOME_ACTION, data } } But i want it to be possible for the fetchData action creator to be reusable so that

RTCPeerConnection and createDataChannel not working for Edge

故事扮演 提交于 2019-12-13 17:24:38
问题 I have been working on an application where when a person logs into an account, the IP address of the device is stored in the backend and local storage. Then when a person logs into the same account from another browser or so, it will show a popup with the last login IP address. It seems to work on chrome and Mozilla but not working in edge browser. It always returns null because it's not entering the code after pc.createDataChannel. The code snippet is as below. const getIpAddress = state =>

React: How to wrap component with HOC? (access login property across components)

孤人 提交于 2019-12-13 16:12:17
问题 I have a react (with redux & react-router) app that at various points needs to know whether or not a user is logged in based on the state of some data in the store and a bunch of other things tied to data in the store. To achieve this, I've created a higher order component like so: import React from 'react'; import {connect} from 'react-redux'; export function masterComponent(Component){ class MasterComponent extends React.Component { constructor(props){ super(props); } loggedIn(){ return