简体   繁体   English

如何在组件中接收先前发送到 redux 存储的值。 (始终未定义)

[英]How to receive in a component a value previously dispached to redux store. (always undefined)

Hi I am sending the value from a select to a redux Store:嗨,我正在将值从 select 发送到 redux 商店:

import React, { Component } from 'react';
import { Select } from 'antd';
import { connect } from "react-redux";
import * as actions from '../store/actions/select';

class SelecionarCrypto extends Component {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);
    this.onBlur = this.onBlur.bind(this);
    this.onFocus = this.onFocus.bind(this);
    this.onSearch = this.onSearch.bind(this);

    console.log("(SelecionarCryto.js):",this.props);

    this.state = {
      ValorState: "nada"
    }


  };

 onChange(value) {
    console.log(`selected ${value}`);
    this.setState({ValorState: value});
    console.log("(SelecionarCryto.js)  New value onchange", this.state.ValorState)
    this.props.onSelectCrypto(value)

  }

  onBlur() {
    console.log('blur');
  }

  onFocus() {
    console.log('focus');
  }

  onSearch(val) {
    console.log('search:', val);
  }



render(){


const { Option } = Select;

console.log("(SelecionarCryto.js) New value Render: ", this.state.ValorState)








return (
  <Select
    showSearch
    style={{ width: 200 }}
    placeholder="Seleciona:"
    optionFilterProp="children"
    onChange={this.onChange}
    onFocus={this.onFocus}
    onBlur={this.onBlur}
    onSearch={this.onSearch}
    filterOption={(input, option) =>
      option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  >
    <Option value="ETH">ETH</Option>
    <Option value="BTC">BTC</Option>
    <Option value="XRP">XRP</Option>
  </Select>

  );
}


  }

  const mapStateToProps = state => {
    return {
      token: state.token,
      selectvalue: state.value

    };
  };

  //Dispaching to STORE:
const mapDispatchToProps = (dispatch) =>{
  return{
    onSelectCrypto: (value) => dispatch(actions.SelectCrypto(value))
  }
};

  export default connect(mapStateToProps, mapDispatchToProps)(SelecionarCrypto); 

Using the console I see how is working and the action is changin every time when I am using the select.使用控制台,我看到每次使用 select 时的工作方式和操作都在变化。

在此处输入图像描述

This parte ot the code i where I am conecting to the store to receive the props:这部分代码是我连接到商店以接收道具的地方:

const mapStateToProps = state => {
  console.log("recibe mapStateToProps: ", state)
  return {
    token: state.token,
    ValorState: state.ValorState,
    username: state.username,
    selectvalue: state.value  // <<<---- This one 

  };
};

This is the props recived:这是收到的道具:

在此处输入图像描述

Why i always undefined?为什么我总是未定义? If my component is connected to the store when the value changes is not supposed to get the value?如果我的组件在值更改时连接到存储不应该获取值?

Information about the reducer code: (parte os the code: )关于减速器代码的信息:(部分代码:)

const SelectCryptoValue = (state, action) => {  //  <--- This one.
    console.log("necesito esto:", state, " ", action)
    return updateObject(state, {
        selectvalue: action.value,
        error: null,
        loading: false

    });
}

const reducer = (state=initialState, action) => {
    switch (action.type) {
        case actionTypes.AUTH_START: return authStart(state, action);
        case actionTypes.AUTH_SUCCESS: return authSuccess(state, action);
        case actionTypes.AUTH_SUCCESS_USERNAME: return authSuccessUsername(state, action);
        case actionTypes.AUTH_FAIL: return authFail(state, action);
        case actionTypes.AUTH_LOGOUT: return authLogout(state, action);
        case actionTypes.SELECT_CRYPTO: return SelectCryptoValue(state, action);
        default:
            return state;
    }
}

export default reducer;

Without having any idea of how your reducer is implemented it's quite hard to say but I feel there is two possibilities:如果不知道减速器是如何实现的,很难说,但我觉得有两种可能性:

  • You are not defining properly the selectValue in your reducer您没有在减速器中正确定义 selectValue
  • You define it properly but using a mutation therefore not triggering a re-rendering of your react components您正确定义它但使用突变因此不会触发您的反应组件的重新渲染

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Redux,我的组件未连接到Redux Store。 我在另一个组件中测试了动作和减速器,它是否对其他组件有效? - React Redux, my component is not connected to Redux Store. I tested the action and reducer in another component, it does work for other components? 使用Redux获取组件上的商店价值 - Get a store value on a component with Redux redux bindActionCreators分配了任何动作 - redux bindActionCreators any action is dispached 我有一个组件正在从商店接收数据。 如何更新收到的数据并在更改时重新呈现组件 - I have a component which is receiving data from the store. How can I update the data received and rerender the component on change 如何为每个商店自动增加特定的数据库表值。 (Laravel 6) - How to auto increment a specific database table value for every store. (Laravel 6) 为什么 React Redux context.store 在子组件中未定义? - Why React Redux context.store is undefined in child component? Redux-Toolkit state 未从商店传递。 react-redux - Redux-Toolkit state not passed from the store. react-redux 如何使用钩子复制 Redux 存储在组件 state 中? - How to copy Redux store in component state with hooks? 如何使用商店 redux 更新本机反应组件 - How to update component in react native with a store redux 如何从组件外部从Redux存储中获取值? - How can I grab a value from a Redux store from outside of a component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM