无法在无状态组件 reactjs 中获取更新 redux 状态
unable to fetch update redux state in state less component reactjs
在我的 reactJs 应用程序中,我正在使用 redux-thunk,当我更新 redux 状态时,我的无状态组件没有获得更新的状态值。
我的 Addons.js
import React from 'react';
import {connect} from 'react-redux';
import {addOnSelected, addonsCount, updateVehData} from '../../actions'
import { Card,Row,Col,Avatar } from 'antd';
import { Collapse} from 'antd';
const Panel = Collapse.Panel;
import HelperImg from '../../../public/images/Helper.png'
const Addons = (props)=>{
console.log(props.VehcileData,"asd")
let addonsCards = [];
const addonsCard = (addon)=>{
addonsCards.push(
<Col lg={{span:7,offset:1}} key={addon.text} style={{paddingBottom:'2rem'}}>
<Card
type="inner"
// title="Small Vehicle"
id='addonCard'
hoverable
onClick={(e)=>{(e.target.id !== 'minus' && e.target.id !== 'adQty' && e.target.id !== 'plus') && props.updateVehData(addon) }}
className={`${addon.selected ? 'addSelected' :''} ad_card`}
>
<Row type="flex" justify="space-around" align="middle" gutter={16}>
<Col lg={{span:16}} xs={{span:24}} style={{padding:'0px'}} >
<div className='ad_img' align='middle'>
<Avatar src={HelperImg} />
</div>
<div align='middle'>
<h3 className='ad_title'>{addon.text}</h3>
</div>
</Col>
<Col lg={{span:16,offset:5}} xs={{span:16,offset:5}}>
<div className="input-group">
<input type="button" value=" - " id='minus' className="button-minus" onClick={()=>props.addonsCount(addon)}/>
<input type="number" className="quantity-field" id='adQty'/>
<input type="button" value="+" className="button-plus" id="plus"/>
</div>
</Col>
<Col lg={{span:24}} xs={{span:24}} className='price'>
<div className='ad_price'>
<h3>Price RS 2000</h3>
</div>
</Col>
</Row>
</Card>
</Col>)
}
const loopVData =()=>{
for (const key of Object.keys(props.VehcileData)) {
if(props.VehcileData[key].vid === props.VSelected){
props.VehcileData[key].mand.map((addon,i)=>{
addonsCard(addon)
})
props.VehcileData[key].pre.map((addon,i)=>{
addonsCard(addon)
})
props.VehcileData[key].optn.map((addon,i)=>{
addonsCard(addon)
})
}
}
}
return(
<div>
<Collapse defaultActiveKey={['1','2']} >
<Panel header="SELECT ADDONS" key="1">
<Row >
{loopVData()}
{addonsCards}
</Row>
</Panel>
</Collapse>
</div>
)
}
const mapStateToProps = (state) =>{
console.log(state.StepThreeReducer.vehcileData)
return{
VehcileData:state.StepThreeReducer.vehcileData,
VSelected:state.StepThreeReducer.isAmbSel
}
}
export default connect(mapStateToProps,{addOnSelected,addonsCount,updateVehData})(Addons);
我的ACTION.js
export function updateVehData(val){
return function(dispatch,getState){
if(val.type === 'opt'){
let VehData = getState().StepThreeReducer.vehcileData;
let vehID =getState().StepThreeReducer.isAmbSel;
for (const key of Object.keys(VehData)) {
// console.log(VehData[key])
if(VehData[key].vid === vehID){
VehData[key].optn.forEach(addon=>{
if(addon._id==val._id){
addon.selected = !addon.selected;
if(addon.selected === true){
addon.qty=1;
}else if(addon.selected === true){
addon.qty=0;
}
}
});
}
}
dispatch({type:VEHICLEDATA,payload:VehData})
}
}
}
我的Reducer.js
import {ISAMBSEL,ADDONSEL,TOTALPRICE,VEHICLEDATA} from '../actions/types';
const INITIAL_STATE = {
isAmbSel:null,
addonSel:[],
vehcileData:0,
};
export default (state=INITIAL_STATE,action) => {
// console.log(action,'reducer')
switch(action.type) {
case ISAMBSEL:
return {...state,isAmbSel:action.payload};
case ADDONSEL:
return {...state,addonSel:[...action.payload]}
case TOTALPRICE:
return {...state,totalPrice:action.payload}
case VEHICLEDATA:{
return {...state,vehcileData:action.payload}}
default:
return state;
}
}
在我的 mapStateToProps
函数中,如果我控制 state.StepThreeReducer.vehcileData
我正在获取更新的值,但是当我在 Addons 组件中控制值时,它没有显示props.VehcileData
值即使在更新 redux 状态后也不会呈现
但同时它的父组件显示更新的 redux 状态值如果我通过 父组件发送更新的状态值 到我的 Addons 组件 然后它也没有显示 redux 状态的更新值。
我想我在 Actions 或 Reducer 中没有任何问题,因为在 mapStateToProps
中它显示实时 redux 状态值。
let VehData = getState().StepThreeReducer.vehcileData;
let vehID = getState().StepThreeReducer.isAmbSel;
for (const key of Object.keys(VehData)) {
// console.log(VehData[key])
if (VehData[key].vid === vehID) {
VehData[key].optn.forEach(addon => {
if (addon._id == val._id) {
addon.selected = !addon.selected;
if (addon.selected === true) {
addon.qty = 1; // <-- mutating
} else if (addon.selected === true) {
addon.qty = 0; // <-- mutating
}
}
});
}
}
当调用 mapStateToProps 时,它会检查 b/w 你的 prevState
和 nextState
的差异,以便它可以决定组件 是否需要更新。
你的情况你只是在这里改变状态,在 reducer 中更新后 prevState
将与你在 reducer 中更新的相同。
您应该尝试深度克隆它或更改程序。
also you should you an middleware (redux-immutable-state-invariant)
which will detect mutation in redux and throws warnings.
在我的 reactJs 应用程序中,我正在使用 redux-thunk,当我更新 redux 状态时,我的无状态组件没有获得更新的状态值。
我的 Addons.js
import React from 'react';
import {connect} from 'react-redux';
import {addOnSelected, addonsCount, updateVehData} from '../../actions'
import { Card,Row,Col,Avatar } from 'antd';
import { Collapse} from 'antd';
const Panel = Collapse.Panel;
import HelperImg from '../../../public/images/Helper.png'
const Addons = (props)=>{
console.log(props.VehcileData,"asd")
let addonsCards = [];
const addonsCard = (addon)=>{
addonsCards.push(
<Col lg={{span:7,offset:1}} key={addon.text} style={{paddingBottom:'2rem'}}>
<Card
type="inner"
// title="Small Vehicle"
id='addonCard'
hoverable
onClick={(e)=>{(e.target.id !== 'minus' && e.target.id !== 'adQty' && e.target.id !== 'plus') && props.updateVehData(addon) }}
className={`${addon.selected ? 'addSelected' :''} ad_card`}
>
<Row type="flex" justify="space-around" align="middle" gutter={16}>
<Col lg={{span:16}} xs={{span:24}} style={{padding:'0px'}} >
<div className='ad_img' align='middle'>
<Avatar src={HelperImg} />
</div>
<div align='middle'>
<h3 className='ad_title'>{addon.text}</h3>
</div>
</Col>
<Col lg={{span:16,offset:5}} xs={{span:16,offset:5}}>
<div className="input-group">
<input type="button" value=" - " id='minus' className="button-minus" onClick={()=>props.addonsCount(addon)}/>
<input type="number" className="quantity-field" id='adQty'/>
<input type="button" value="+" className="button-plus" id="plus"/>
</div>
</Col>
<Col lg={{span:24}} xs={{span:24}} className='price'>
<div className='ad_price'>
<h3>Price RS 2000</h3>
</div>
</Col>
</Row>
</Card>
</Col>)
}
const loopVData =()=>{
for (const key of Object.keys(props.VehcileData)) {
if(props.VehcileData[key].vid === props.VSelected){
props.VehcileData[key].mand.map((addon,i)=>{
addonsCard(addon)
})
props.VehcileData[key].pre.map((addon,i)=>{
addonsCard(addon)
})
props.VehcileData[key].optn.map((addon,i)=>{
addonsCard(addon)
})
}
}
}
return(
<div>
<Collapse defaultActiveKey={['1','2']} >
<Panel header="SELECT ADDONS" key="1">
<Row >
{loopVData()}
{addonsCards}
</Row>
</Panel>
</Collapse>
</div>
)
}
const mapStateToProps = (state) =>{
console.log(state.StepThreeReducer.vehcileData)
return{
VehcileData:state.StepThreeReducer.vehcileData,
VSelected:state.StepThreeReducer.isAmbSel
}
}
export default connect(mapStateToProps,{addOnSelected,addonsCount,updateVehData})(Addons);
我的ACTION.js
export function updateVehData(val){
return function(dispatch,getState){
if(val.type === 'opt'){
let VehData = getState().StepThreeReducer.vehcileData;
let vehID =getState().StepThreeReducer.isAmbSel;
for (const key of Object.keys(VehData)) {
// console.log(VehData[key])
if(VehData[key].vid === vehID){
VehData[key].optn.forEach(addon=>{
if(addon._id==val._id){
addon.selected = !addon.selected;
if(addon.selected === true){
addon.qty=1;
}else if(addon.selected === true){
addon.qty=0;
}
}
});
}
}
dispatch({type:VEHICLEDATA,payload:VehData})
}
}
}
我的Reducer.js
import {ISAMBSEL,ADDONSEL,TOTALPRICE,VEHICLEDATA} from '../actions/types';
const INITIAL_STATE = {
isAmbSel:null,
addonSel:[],
vehcileData:0,
};
export default (state=INITIAL_STATE,action) => {
// console.log(action,'reducer')
switch(action.type) {
case ISAMBSEL:
return {...state,isAmbSel:action.payload};
case ADDONSEL:
return {...state,addonSel:[...action.payload]}
case TOTALPRICE:
return {...state,totalPrice:action.payload}
case VEHICLEDATA:{
return {...state,vehcileData:action.payload}}
default:
return state;
}
}
在我的 mapStateToProps
函数中,如果我控制 state.StepThreeReducer.vehcileData
我正在获取更新的值,但是当我在 Addons 组件中控制值时,它没有显示props.VehcileData
值即使在更新 redux 状态后也不会呈现
但同时它的父组件显示更新的 redux 状态值如果我通过 父组件发送更新的状态值 到我的 Addons 组件 然后它也没有显示 redux 状态的更新值。
我想我在 Actions 或 Reducer 中没有任何问题,因为在 mapStateToProps
中它显示实时 redux 状态值。
let VehData = getState().StepThreeReducer.vehcileData;
let vehID = getState().StepThreeReducer.isAmbSel;
for (const key of Object.keys(VehData)) {
// console.log(VehData[key])
if (VehData[key].vid === vehID) {
VehData[key].optn.forEach(addon => {
if (addon._id == val._id) {
addon.selected = !addon.selected;
if (addon.selected === true) {
addon.qty = 1; // <-- mutating
} else if (addon.selected === true) {
addon.qty = 0; // <-- mutating
}
}
});
}
}
当调用 mapStateToProps 时,它会检查 b/w 你的 prevState
和 nextState
的差异,以便它可以决定组件 是否需要更新。
你的情况你只是在这里改变状态,在 reducer 中更新后 prevState
将与你在 reducer 中更新的相同。
您应该尝试深度克隆它或更改程序。
also you should you an middleware (redux-immutable-state-invariant) which will detect mutation in redux and throws warnings.