【问题标题】:I want to display a list created by the user / React Native + Redux我想显示用户创建的列表/React Native + Redux
【发布时间】:2018-01-01 18:45:36
【问题描述】:

我希望我的应用程序使用 Redux 显示用户在其下的标签中输入的内容。

这是我的容器:

    const mapStateToProps = state => ({
  text: state
})

const mapDispatchToProps = (dispatch) => ({
  addToList: () => { dispatch({ type: 'ADD_LIST' }) },
})

export default connect(mapStateToProps, mapDispatchToProps)(TodoList)

这是我的组件:

class TodoList extends Component {
  render() {
    return (
      <View>
        <TextInput
          style={{height: 40, width: 300}}
          placeholder="Type here to translate!"
          onChangeText={(text) => this.props.text}
        />
        <Button
          title="Submit"
          onPress={this.props.addToList}/>
        <View>
          <Text>{this.props.text}</Text>
        </View>
      </View>
    )
  }
}



export default TodoList;

这里是商店:

export const todoList = (state = [], action = {}) => {
  switch (action.type) {
  case 'ADD_LIST':
    return [
        ...state,
        action.todo
    ];
  default:
    return state;
  }
}

let storeTodoList = createStore(todoList);

export default storeTodoList;

所以我正在尝试输入文本,将其添加到存储在商店中的列表中,然后显示它,但我完全不知道如何做到这一点......

【问题讨论】:

  • 您在调用容器中的dispatch 时缺少有效负载
  • 我不明白那里缺少什么。抱歉,从今天开始,我是 React Native 的初学者......

标签: android ios react-native redux react-redux


【解决方案1】:

这里发生了一些事情......

  1. 你的onChangeText 监听器没有做任何事情。您需要捕获输入到组件中的文本并将其发送给您的调度员。
  2. 您需要包含作为动作创建者的一部分传入的新文本。
  3. mapStateToProps 负责获取处于应用程序状态的元素并将其映射到 props 以供您的组件使用。对于此示例,您的应用程序状态非常简单。只是{ text: 'SOME TEXT' }
  4. 你需要为你的 Redux store 创建一个Provider。它应该在您应用的根级别运行。

以下是所有部分:

App.js(创建 Provider 的应用控制器)

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import todoList from './actions/Reducer';
import { createStore } from 'redux';
import Root from './Root';

class App extends Component {
  store = createStore(todoList);

  render() {
    return (
        <Provider store={this.store}>
          <Root/>
        </Provider>
    )
  }
}

export default App;

Root.js

import React, { Component } from 'react';
import { View, TextInput, Button, Text } from 'react-native';
import { connect } from 'react-redux';

class Root extends Component {
  render() {
    const displayText = this.props.textList.join();

    return (
        <View>
          <TextInput
              style={{height: 40, width: 300}}
              placeholder="Type here to translate!"
              onChangeText={(text) => this.props.updateField(text)}
          />
          <Button
              title="Submit"
              onPress={() => this.props.addToList(this.props.text)}/>
          <View>
            <Text>{displayText}</Text>
          </View>
        </View>
    )
  }
}

const mapStateToProps = state => ({
  text: state.textField,
  textList: state.list
});

const mapDispatchToProps = (dispatch) => ({
  updateField: (newText) => { dispatch({ type: 'FIELD_CHANGE', text: newText })},
  addToList: (text) => { dispatch({ type: 'ADD_LIST', text: text }) },
});

export default connect(mapStateToProps, mapDispatchToProps)(Root)

Reducer.js(控制你的状态对象)

const INITIAL_STATE = {
  textField: '',
  list: []
};

export default todoList = (state = INITIAL_STATE, action = {}) => {
  switch (action.type) {
    case 'FIELD_CHANGE':
      return {
        ...state,
        textField: action.text
      };
    case 'ADD_LIST':
      return {
        textField: '',
        list: [...state.list, action.text]
      };
    default:
      return state;
  }
};

编辑更改示例以添加到列表中。注意:这不是在 RN 中显示项目列表的正确方式。我只是将字符串放入示例中的 Text 字段中。使用FlatList 正确显示项目列表。

【讨论】:

  • 抱歉,我花了这么多时间回复,这很好用!我还有一个问题,当我单击提交时,如何将值存储到列表中,然后显示它。我现在尝试了几个小时,但我仍然无法使其工作。感谢您花时间帮助我:)
  • 更新示例以在单击提交时将字段附加到列表中。请务必阅读我在 FlatList 上的说明。
  • 感谢您抽出宝贵时间。我将进一步使其与 FlatList 一起工作。你让我在这里明白了很多事情!
猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
相关资源
最近更新 更多