【问题标题】:Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method React Native传播不可迭代实例的无效尝试。为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法 React Native
【发布时间】:2020-09-17 11:11:19
【问题描述】:

我正在尝试测试一个 useState 值和对象,以与 redux 进行本机反应。 当我必须使用 react-Redux 将值存储在 state 中时出现此错误..
我想用 react native redux 将笔记和数字存储在两个 arrasy 中,并通过单个 store 执行这两个操作。非常感谢任何帮助

组件:

import React, { useState } from 'react'
import { View, StyleSheet } from 'react-native'
import { IconButton, TextInput, FAB } from 'react-native-paper'
import Header from '../components/Header'

function AddNote({ navigation }) {
  const [noteTitle, setNoteTitle] = useState('')
  const [noteValue, setNoteValue] = useState('')

  function onSaveNote() {
    navigation.state.params.addNote({ noteTitle, noteValue })
    navigation.goBack();
  }
  return (
    <>
      <Header titleText='Add a new note' />
      <IconButton
        icon='close'
        size={25}
        color='white'
        onPress={() => navigation.goBack()}
        style={styles.iconButton}
      />
      <View style={styles.container}>
        <TextInput
          label='Add Title Here'
          value={noteTitle}
          mode='outlined'
          onChangeText={setNoteTitle}
          style={styles.title}
        />
        <TextInput
          label='Add Note Here'
          value={noteValue}
          onChangeText={setNoteValue}
          mode='flat'
          multiline={true}
          style={styles.text}
          scrollEnabled={true}
          returnKeyType='done'
          blurOnSubmit={true}
        />
        <FAB
          style={styles.fab}
          small
          icon='check'
          disabled={noteTitle == '' ? true : false}
          onPress={() => onSaveNote()}
        />
      </View>
    </>
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    paddingHorizontal: 20,
    paddingVertical: 20
  },
  iconButton: {
    backgroundColor: 'rgba(46, 113, 102, 0.8)',
    position: 'absolute',
    right: 0,
    top: 40,
    margin: 10
  },
  title: {
    fontSize: 24,
    marginBottom: 20
  },
  text: {
    height: 300,
    fontSize: 16
  },
  fab: {
    position: 'absolute',
    margin: 20,
    right: 0,
    bottom: 0
  }
})

export default AddNote

减速器:

import remove from 'lodash.remove'

// Action Types

export const ADD_NOTE = 'ADD_NOTE'
export const DELETE_NOTE = 'DELETE_NOTE'
export const ADD_NUMBER = 'ADD_NUMBER'
export const DELETE_NUMBER = 'DELETE_NUMBER'
    
// Action Creators

let noteID = 0
let numberID = 0

export function addnote(note) {
  return {
    type: ADD_NOTE,
    id: noteID++,
    note
  }
}

export function deletenote(id) {
  return {
    type: DELETE_NOTE,
    payload: id
  }
}

export function addnumber(number) {
  return {
    type: ADD_NUMBER,
    id: numberID++,
    number
  }
}

export function deletenumber(id) {
  return {
    type: DELETE_NUMBER,
    payload: id
  }
}

// reducer    
const INITIAL_STATE = {
  note: [],   //note array for save notes
  number: []   // number array for save numbers
};
function notesReducer(state = INITIAL_STATE, action) {
  switch (action.type) {
    case ADD_NOTE:
      return [
        ...state,
        {
          id: action.id,
          note: action.note
        }
      ]

    case DELETE_NOTE:
      const deletedNewArray = remove(state, obj => {
        return obj.id != action.payload
      })
      return deletedNewArray

    case ADD_NUMBER:
      return [
        ...state,
        {
          id: action.id,
          number: action.number
        }
      ]

    case DELETE_NUMBER:
       deletedNewArray = remove(state, obj => {
        return obj.id != action.payload
      })
      return deletedNewArray

    default:
      return state
  }
}

export default notesReducer    

那我该怎么办.. 请帮帮我..

【问题讨论】:

    标签: reactjs react-native redux react-redux


    【解决方案1】:

    reducer 必须返回新状态而不是更新的参数:

    // reducer    
    const INITIAL_STATE = {
      note: [],   //note array for save notes
      number: []   // number array for save numbers
    };
    
    function notesReducer(state = INITIAL_STATE, action) {
      switch (action.type) {
        case ADD_NOTE:
          return {
            ...state,
            note: [
               ...state.note,
               {
                   id: action.id,
                   note: action.note
               }
            ]
          };
    
        case DELETE_NOTE:
          const note = remove(state.note, obj => obj.id != action.payload);
    
          return {...state, note};
    
        case ADD_NUMBER:
          return {
            ...state,
            number: [
                ...state.number,
                {
                  id: action.id,
                  number: action.number
                }
            ]
          };
    
        case DELETE_NUMBER:
           const number = remove(state.number, obj => obj.id != action.payload);
    
           return {...state, number}
    
        default:
          return state
      }
    }
    

    【讨论】:

    • 它没有显示任何错误,但也没有显示数字列表和注释列表..
    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 2023-01-23
    • 2019-07-01
    • 2019-06-25
    相关资源
    最近更新 更多