【问题标题】:Pass input value to store using react-redux payload and reducer使用 react-redux 有效负载和 reducer 将输入值传递给存储
【发布时间】:2021-08-27 23:19:38
【问题描述】:

我有一个文本输入,我正在尝试将输入的值发送到我的减速器中的商店,然后在屏幕上显示该值。

似乎输入值甚至没有进入商店...有人可以解释为什么我没有正确发送这个吗?

e.target.value 抛出未定义的错误。

组件

import React, { Component,setState } from 'react';
import { StyleSheet, View, Button, Text, StatusBar, TextInput, TouchableOpacity, Image } from 'react-native';

import { connect } from 'react-redux';


class Counter extends Component {

  emailHandler() {
      this.props.email();
  }

  toggleCounterHandler() {}

  render() {
    return (
      <View>
       
        
        <View ><Text style={styles.welcometext}>{this.props.value}</Text></View>
        <View>
          
            <TextInput
             style={styles.input}
             onChangeText= {(e) => this.props.emailHandler(e.target.value)} 
             value={this.props.value}
             secureTextEntry={false}
             />
        </View>
      </View>
    );
  }
}

const mapStateToProps = (state) => {
    return {
      value: state.value
    };
  }
  
  const mapDispatchToProps = dispatch => {
    return {
      email: (value) => dispatch({
          type: 'email',
          payLoad: value
        })
      //() => dispatch({type: 'email', text: this.state.text})
    }
  };
  
export default connect(mapStateToProps, mapDispatchToProps)(Counter);

减速器

import React from 'react';
import { createStore } from 'redux';

const counterReducer = (state = { email:'email' }, action) => {

  if (action.type === 'email') {
    return {
    ...state,
      email: action.payload,
    };
  }
  

console.log(state);
  return state;
};

const store = createStore(counterReducer);

export default store;

【问题讨论】:

    标签: react-native react-redux textinput reducers


    【解决方案1】:

    如果您查看docs 中的onChangeText api,它会直接将文本作为参数传递。因此,您不必使用e.target.value

    第二件事,我注意到你mapDispatchToPropsemail 而不是emailHandler?所以你的redux函数应该是this.props.email

    你不必创建另一个函数 emailHandler 来使用 props 中的 redux

    你应该这样做:

    import React, { Component,setState } from 'react';
    import { StyleSheet, View, Button, Text, StatusBar, TextInput, TouchableOpacity, Image } from 'react-native';
    
    import { connect } from 'react-redux';
    
    
    class Counter extends Component {
    
    
      toggleCounterHandler() {}
    
      render() {
        return (
          <View>
           
            
            <View ><Text style={styles.welcometext}>{this.props.value}</Text></View>
            <View>
              
                <TextInput
                 style={styles.input}
                 onChangeText= {(e) => this.props.email(e.target.value)} 
                 value={this.props.value}
                 secureTextEntry={false}
                 />
            </View>
          </View>
        );
      }
    }
    
    const mapStateToProps = (state) => {
        return {
          value: state.value
        };
      }
      
      const mapDispatchToProps = dispatch => {
        return {
          email: (value) => dispatch({
              type: 'email',
              payLoad: value
            })
          //() => dispatch({type: 'email', text: this.state.text})
        }
      };
      
    export default connect(mapStateToProps, mapDispatchToProps)(Counter);
    

    【讨论】:

    • 我试过了,我收到:“_this2.props.emailHandler 不是一个函数。”我还有什么遗漏的吗?
    • 我需要将 myText 作为接收到 emailHander(myText) {} 的输入吗?
    • 查看更新后的答案,您的mapDispatchToProps 不是emailHandler。您甚至不必创建另一个函数来执行此操作,只需直接传递它
    • 所以我仍然会调用 onChangeText= {(myText) => this.props.email(myText)} 那么?
    • 当我使用提供的方法时,我在减速器中收到 undefined 作为值。错误消失了,但似乎没有发送值。
    猜你喜欢
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多