简体   繁体   English

调用Redux操作时道具不更新

[英]Props not being updated when Redux action is called

Just a disclaimer - my code worked when I had problematic function in my main component. 只是一个免责声明-当我的主要组件中的函数出现问题时,我的代码就会起作用。 After I exported it it stopped behaving as it should. 我将其导出后,它停止了应有的运行。 My theory is because somehow props are not being updated properly. 我的理论是因为道具无法正确更新。

Anyway, I have a component, which after it's clicked it starts listening on window object and sets proper store element to "true" and depending on next object clicked acts accordingly. 无论如何,我都有一个组件,单击该组件后,它将开始监听窗口对象,并将适当的存储元素设置为“ true”,并根据单击的下一个对象进行相应的操作。 After incorrect object is clicked, the store should revert to false, and it does, however the props are still "true" as shown on the screenshot below. 单击不正确的对象后,商店应还原为false,并且确实如此,但是道具仍然是“ true”,如下面的屏幕截图所示。

How can I solve this? 我该如何解决? Perhaps there is a way that function could take store as parameter instead of props? 也许有一种方法可以将函数存储为参数而不是props? Or im calling actions inproperly or im missing something completely? 还是即时呼叫动作不正确或完全缺少某些内容?

Code below: 代码如下:

Main component (relevant parts?): 主要组成部分(相关零件?):

import React from 'react';
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'

import {activate} from '../../actions/inventory'
import { setModalContent, setModalState } from '../../actions/modal';
import inventoryReducer from '../../reducers/inventory';

import {chainMechanics} from './itemMechanics/chainMechanics';


class ItemRenderer extends React.Component{

    handleBoltcuttersClicked(){
        this.props.activate('boltcutters', true);
        setTimeout(() => chainMechanics(this.props), 100)
    }

    inventoryItemRender(){
        let inventoryItem = null;
        if(this.props.inventory.items.boltcutters){
            inventoryItem = <a className={this.props.inventory.activeItem.boltcutters ? "ghost-button items active " : "ghost-button items"} href="#" id='boltcuttersId' onClick={() => this.handleBoltcuttersClicked()}>Boltcutters</a>
        }
        return inventoryItem;
    }

    render(){
        let renderItems = this.inventoryItemRender();
        return(
            <div>
                {renderItems}
            </div>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        level: state.level,
        inventory: state.inventory
    }
}

function mapDispatchToProps(dispatch) {
    //dispatch w propsach
    return(
        bindActionCreators({activate: activate, setModalState: setModalState, setModalContent: setModalContent }, dispatch)
    ) 
  }

export default connect(mapStateToProps, mapDispatchToProps)(ItemRenderer);

File with problematic function: 功能有问题的文件:

import {activate} from '../../../actions/inventory'
import { setModalContent, setModalState } from '../../../actions/modal';

export function chainMechanics(props){
    let clickedElement;
    window.onclick = ((e)=>{
        console.log(clickedElement, 'clickedelement', props.inventory.activeItem.boltcutters)
        if(props.inventory.activeItem.boltcutters===true){
            clickedElement = e.target;
            if(clickedElement.id === 'chainChainedDoor'){
                props.activate('boltcutters', false);
                props.setModalContent('Chain_Broken');
                props.setModalState(true);
            } else if(clickedElement.id === 'boltcuttersId'){
                console.log('foo')
            } else  {
                props.activate('boltcutters', false);
                props.setModalContent('Cant_Use');
                props.setModalState(true);
                console.log("props.inventory.activeItem.boltcutters", props.inventory.activeItem.boltcutters); 
            }
        }
    })
}

My actions: 我的动作:

const inventoryReducer = (state = inventoryDefaultState, action) => {
    switch (action.type) {
        case 'ACTIVE':
            console.log(action)
            return {
                ...state,
                activeItem: {
                    ...state.activeItem,
                    [action.item]: action.isActive
                }
            }
        default:
            return state;
    }
}

How I configure store: 我如何配置商店:

export default () => {
    const store = createStore(
        combineReducers({
            level: levelReducer,
            modal: modalReducer,
            inventory: inventoryReducer,
            styles: stylesReducer
        }),
        applyMiddleware(thunk)
    )
    return store;
}

I believe thats eveyrthing needed? 我相信需要一切吗? If not please do let me know, I've been trying to make this work for a long time. 如果没有,请告诉我,我已经尝试了很长时间了。

Screenshot: 屏幕截图: 控制台日志

You can use the React's function componentWillReceiveProps . 您可以使用React的函数componentWillReceiveProps That would trigger a rerender like this (and also make use of next props/state): 这将触发这样的重新渲染(并利用下一个道具/状态):

componentWillReceiveProps(next) {
    console.log(next);
    this.inventoryItemRender(next);
}

inventoryItemRender(next){
        const inventory = next.inventory ? next.inventory : this.props.inventory;
        let inventoryItem = null;
        if(inventory.items.boltcutters){
            inventoryItem = <a className={inventory.activeItem.boltcutters ? "ghost-button items active " : "ghost-button items"} href="#" id='boltcuttersId' onClick={(next) => this.handleBoltcuttersClicked(next)}>Boltcutters</a>
        }
        return inventoryItem;
}

handleBoltcuttersClicked(props){
        this.props.activate('boltcutters', true);
        setTimeout(() => chainMechanics(props), 100)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM