简体   繁体   English

React/Redux 点击时激活多个组件

[英]React/Redux Multiple components activatated on click

i'm studying React/Redux and i tried to do something on my own besides courses.我正在学习 React/Redux,除了课程之外,我还尝试自己做一些事情。 So i'm replicating an atlas from a game i play called Path of Exile, but with some changes.所以我从我玩的名为“流放之路”的游戏中复制了一个地图集,但有一些变化。 For example, the main problem i'm trying to "solve" is to click on a map and it highlights it showing that map it's completed, and if you click it again that map will uncomplete.例如,我要“解决”的主要问题是单击 map 并突出显示 map 它已完成,如果再次单击它,Z1D78DC8ED51214E518B5114FE2449AEZ 将未完成。 like the examples in the imgs:就像imgs中的例子:

Completed Maps已完成的地图

Uncompleted Maps未完成的地图

My problem is that when i click on one map, all other maps are marking as completed.我的问题是,当我点击一个 map 时,所有其他地图都标记为已完成。 On the images there are only 2 maps but there is 156 maps and obviously i don't want to click on map X and highlight all other maps.在图像上只有 2 张地图,但有 156 张地图,显然我不想点击 map X 并突出显示所有其他地图。

This is the component i load each Map:这是我加载每个 Map 的组件:

export default props => (
    <div className="regions">
        <Region region="haewark_hamlet">
            <Map
                map_title="Bog"
                map_name="bog"
                white_map={maps.haewark.bog.white}
                yellow_map={maps.haewark.bog.yellow}
                red_map={maps.haewark.bog.red}
                map_tier="3"
            />
            <Map
                map_title="Vaal Pyramid"
                map_name="vaal_pyramid"
                white_map={maps.haewark.vaal_pyramid.white}
                yellow_map={maps.haewark.vaal_pyramid.yellow}
                red_map={maps.haewark.vaal_pyramid.red}
                map_tier="3"
            />
        </Region>
        <Region region="tirns_end" />
        <Region region="glennach_cairns" />
        <Region region="new_vastir" />
        <Region region="lex_ejoris" />
        <Region region="lex_proxima" />
        <Region region="valdos_rest" />
        <Region region="lira_arthain" />
    </div>
);

This is my Map component:这是我的 Map 组件:

const Map = props => {
    const { completed } = props;

    const toggleComplete = function () {
        completed ? props.mapUncomplete() : props.mapComplete();
    };

    return (
        <div className={`map ${props.map_name}`} id={props.map_name}>
            <div className="map-name">{props.map_title}</div>
            <img className="map-size" src={base_map} alt="base map" />
            <div className="map-tier-color">
                <img
                    className={`map-size ${props.map_name}-white`}
                    src={props.white_map}
                    alt={`${props.map_name} white map`}
                />
                <img
                    className={`map-size ${props.map_name}-yellow`}
                    src={props.yellow_map}
                    alt={`${props.map_name} yellow map`}
                />
                <img
                    className={`map-size ${props.map_name}-red`}
                    src={props.red_map}
                    alt={`${props.map_name} red map`}
                />
            </div>
            <div className="map-tier-number">Tier {props.map_tier}</div>

            <div
                className={`toggle-completed ${completed ? "completed-map" : ""}`}
                onClick={() => toggleComplete()}
            ></div>
        </div>
    );
};

const mapStateToProps = state => ({
    completed: state.poeapp.completed,
});

const mapDispatchToProps = dispatch =>
    bindActionCreators(
        {
            mapComplete,
            mapUncomplete,
        },
        dispatch,
    );

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

this is my Map Actions:这是我的 Map 操作:

export const mapComplete = () => ({
    type: "MAP_COMPLETE",
    payload: true,
});

export const mapUncomplete = () => ({
    type: "MAP_UNCOMPLETE",
    payload: false,
});

this is my Map Reducers:这是我的 Map 减速机:

const INITIAL_STATE = {
    completed: false,
};

export default (state = INITIAL_STATE, action) => {
    switch (action.type) {
        case "MAP_COMPLETE":
            return { ...state, completed: action.payload };
        case "MAP_UNCOMPLETE":
            return { ...state, completed: action.payload };
        default:
            return state;
    }
};

As i said i'm still learning, i'm sure i'm doing something wrong, i just don't know what, If my doubt wasn't clear enough, tell me and i'll try to rewrite it!正如我所说我还在学习,我确定我做错了什么,我只是不知道是什么,如果我的疑问不够清楚,请告诉我,我会尝试重写它! Thank you!谢谢!

If you want to mark just some maps as completed/uncompleted, you should keep in redux a structure to know which ones are completed, and which ones are not.如果您只想将一些地图标记为已完成/未完成,则应在 redux 中保留一个结构,以了解哪些已完成,哪些未完成。

For example, your store should look something like this:例如,您的商店应如下所示:

{
   ...
   completed: [0,2,3,...,78,90],
}

You can asume that if a map ID is not in that array, the map is not completed.您可以假设,如果 map ID 不在该数组中,则 map 未完成。 In such case, your action creators should change also.在这种情况下,您的动作创建者也应该更改。 Instead of just passing if a map is completed or not, you have to provide the identifier of the map you're refering.如果 map 完成与否,您必须提供您所指的 map 的标识符,而不是仅仅通过。

Obviously, it's just an example and it's not the best implementation to solve your problem, indeed.显然,这只是一个示例,实际上并不是解决您的问题的最佳实现。 It's up to you to find the one that fits with your problem.由您决定找到适合您问题的那个。 However, the basic idea will still being the same: you have to provide to your components, action creators and so that your reducers, the identifier of the map you want to apply your action.但是,基本思想仍然是相同的:您必须向您的组件、动作创建者和减速器提供要应用您的动作的 map 的标识符。

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

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