Redux不会一次向商店发送2个属性

t30tvxxf  于 2023-11-19  发布在  其他
关注(0)|答案(1)|浏览(73)

我是Redux的新手。我有一个Reducer,它的初始状态有两个属性。
我从一个组件中发送数据到存储区,但只有一个属性被传递--时间或日期,但我需要同时发送这两个属性。
我试着做了一个编译器/await,但我认为解释器会超时-尽管它没有那么逻辑,我试过了。它没有帮助我。我还认为Redux不能接受对象,但我研究了一下,发现它可以。然后我向自己证明它真的可以-我只是简单地发送了一个属性。
代码片段在下面。

*Reducer: *

const initialState = {
  date: {},
  time: {}
}

const dateTimeReducer = (state = initialState, action) => {
    switch (action.type) {
        case 'setDate':
            return {
                date: action.payload,
            }

        case 'setPickedCell':
            return {
                time: action.payload,
            }

        default:
            return state
    }
}
export default dateTimeReducer
*index.js*

import React from 'react'
import ReactDOM from 'react-dom'
import './css/style.comp.css'
import App from './App'
import { BrowserRouter } from 'react-router-dom'
import store from './redux/store'
import { Provider } from 'react-redux'
// import rootReducer from './redux/rootReducer'

// const store = createStore(rootReducer)

ReactDOM.render(
    <Provider store={store}>
        <BrowserRouter>
            <App />
        </BrowserRouter>
    </Provider>,

    document.getElementById('root')
)
*store.js*

import { createStore } from 'redux'
import rootReducer from './rootReducer'

const store = createStore(rootReducer)

export default store
*rootReducer.js*

import { combineReducers } from 'redux'
import dateTime from './reducers/dateTime'

export default combineReducers({ dateTime })
*Component - I will simplify, with a condition that I have no syntax mistakes and the code works when I send only 1 of 2 properties to a Redux store*

import Date from ''
import Time from ''

class Comp extends React.Component {
  state = {
     date: {...}, 
     time: {...} // both date and time already set during rendering cycle - through a function 
                 // acceptStateFromDate and acceptStateFromTime
  }

  acceptStateFromDate() {
    this.setState({ date: stateData })
  } 

  // same about acceptStateFromTime - those functions are used to get state data from a child component and are called while componentDidMount

  acceptStateFromTime() {
    ...
  }

  onConfirmClick = () => {
    this.props.isAllowedHandler()
    this.props.onDateSet(this.state.datePicker)
    this.props.onCellSet(this.state.timePicker)
  }

  componentDidMount() {
    acceptStateFromDate() 
    acceptStateFromTime()
  } 

  render() {
    return (
      <div> 
        <div onClick={this.onConfirmClick}> Confirm </div>
        <Time sendState={acceptStateFromTime} />
        <Date sendState={acceptStateFromDate} />
    )
  }
}

function mapStateToProps(state) {
    return {
        date: state.dateTime.date,
        time: state.dateTime.time,
    }
}

function mapDispatchToProps(dispatch) {
    return {
        onDateSet: (val) => dispatch({ type: 'setDate', payload: val }),
        onCellSet: (val) => dispatch({ type: 'setPickedCell', payload: val }),
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(withRouter(Appointment))

所有的导出和导入都存在于我的代码中。
所以,如果我通过onConfirmClick只发送this.props.onDateSet({someObj})```, I will get in my Redux state日期:{someObj} . Same about time - I will get时间:{someObj}`。
但是如果我把这两个 prop 放在一起,只有一个会被送到商店。
我想问题比逻辑更深,可能我在React系统或Redux方面都不够好。
有人知道问题出在哪里吗?
提前感谢!

o75abkj4

o75abkj41#

你的reducers是错误的,你必须返回state。目前你完全重写它:

return {
  date: action.payload,
}

字符串
将返回语句更改为以下内容:

return {
  ...state,
  date: action.payload,
}
const dateTimeReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'setDate':
      return {
        ...state,
        date: action.payload,
      }
    case 'setPickedCell':
      return {
        ...state,
        time: action.payload,
      }
    default:
      return state
  }
}

的数据

相关问题