【问题标题】:Send a this.state to Store from component and retrive from another component从组件发送 this.state 到 Store 并从另一个组件检索
【发布时间】:2020-04-19 15:57:59
【问题描述】:

我现在不知道这样做的最佳方法是什么:

我有一个从中您可以选择一种货币的方法,根据您选择的货币,表格会重新呈现为所选货币。

我正在使用来自 mdbreact 的 REACTJS、REDUX 和 MDBDataTable,并且表单正在使用来自 antd 的 Select。

MDBDataTable 正在工作,我可以发送数据,选择也正在工作。我唯一需要的是某种方式来发送我根据这些信息选择货币的信息,我可以构建我的数据发送到我的桌子。

也许这不是正确的想法,我愿意接受任何建议。 (我不是程序员,所以我在寻求帮助)。

我试图将值发送到排序的组件代码(最后):

import React, { Component } from 'react';
import { Select } from 'antd';
import { connect } from "react-redux";

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)

  }

  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,

    };
  };

const mapDispatchToProps = (dispatch) =>{
  return{
    ValorState: "pruebasEnviar"
  }
};

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

这部分只是我试图从我的 STORE 中提取价值的部分。

  const mapStateToProps = state => {
    return {
      token: state.token,
      ValorState: state.ValorState
    };
  };

export default connect(mapStateToProps)(PruebasAPI3);

谢谢,

【问题讨论】:

    标签: javascript reactjs redux setstate


    【解决方案1】:

    您需要向 redux 存储发送一个操作来更新货币,而不是在本地状态中拥有货币状态。

    您的表格组件将从 redux 存储中读取货币状态并相应地呈现表格。

    【讨论】:

    • 嗨,它现在完成了,但正在返回 undefined 。我存储的值是好的,在行动中我看到了值是如何变化的。
    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2021-05-02
    • 2020-07-30
    • 1970-01-01
    • 2021-08-07
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多