Redux 不会一次向商店发送 2 个属性
Redux does not send to the store 2 properties at a time
我是 Redux 新手。我有一个具有 2 个属性的初始状态的减速器。
我从一个组件向存储发送数据,但只传送了一个属性 - 时间或日期。但我需要同时发送。
我试着做 async/await 认为解释器只是用完了时间 - 尽管它不那么合乎逻辑,但我试过了。它对我没有帮助。我还认为 Redux 不能接受对象,但我研究发现它可以。然后我向自己证明它确实可以——我只是简单地只发送了 1 属性.
代码片段如下。
*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}````。
但如果我把这 2 个道具放在一起,只有一个会送到商店。
我想问题比逻辑更深,可能是我在 React 系统或 Redux 方面不够好。
有谁知道问题出在哪里?
提前致谢!
你的减速器是错误的,你也必须 return state
。目前你正在完全重写它:
return {
date: action.payload,
}
将您的 return 语句更改为以下内容:
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
}
}
我是 Redux 新手。我有一个具有 2 个属性的初始状态的减速器。
我从一个组件向存储发送数据,但只传送了一个属性 - 时间或日期。但我需要同时发送。
我试着做 async/await 认为解释器只是用完了时间 - 尽管它不那么合乎逻辑,但我试过了。它对我没有帮助。我还认为 Redux 不能接受对象,但我研究发现它可以。然后我向自己证明它确实可以——我只是简单地只发送了 1 属性.
代码片段如下。
*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}````。
但如果我把这 2 个道具放在一起,只有一个会送到商店。
我想问题比逻辑更深,可能是我在 React 系统或 Redux 方面不够好。
有谁知道问题出在哪里?
提前致谢!
你的减速器是错误的,你也必须 return state
。目前你正在完全重写它:
return {
date: action.payload,
}
将您的 return 语句更改为以下内容:
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
}
}