React 切换显示模式

山姆

我是 React 的新手,我不明白为什么我的显示没有改变(切换)。我有三个按钮,它们应该显示在屏幕上 > 800px屏幕 < 800px 中,只有活动按钮应该是 visibel通过单击活动按钮,它也应该显示其他两个按钮......它第一次从 inlineBlock 切换到无,但之后它对动作没有反应......

import React, {Component} from 'react';

class Tags extends Component {
    constructor(props) {
        super(props);
        this.state = {
            smallScreen: window.innerWidth < 800,
            tags: [
                {"id": 1, "title": "World Of Tanks"},
                {"id": 2, "title": "World Of Warplanes"},
                {"id": 3, "title": "World Of Warship"},
            ],
            activeTags: {"id": 2, "title": "World Of Warplanes"},
            displayingA: "inlineBlock",
            displayingB: "none",
        }
    }

    onClickBtn = (tag) => {
        console.log(this.state.displayingA);
        console.log(this.state.displayingB);
        console.log(this.state.activeTags);

        if (this.state.smallScreen) {
            if (tag.id === this.state.activeTags.id) {
                this.getStyles();
                console.log("change show/hide style");
            } else {
                this.getStyles();
                this.setState({activeTags: tag});
                console.log("make this btn active");
            }
        } else {
            this.setState({activeTags: tag});
            console.log("make this btn active big screen");
        }
    };

    getStyles = () => {
        if (this.state.displayingB === "none") {
            console.log("change show");
            this.setState({displayingB: "inlineBlock"})
        } else {
            console.log("change hide");
            this.setState({displayingB: "none"})
        }
    };

    activeTag = (title) => "-> " + title;

    render() {
        const {tags, activeTags, smallScreen, displayingA, displayingB} = this.state;

        // 1300 px
        const listItem = tags.map((tag) => (
            <button
                style={tag.title !== activeTags.title && smallScreen ? {display: displayingB} : {display: displayingA}}
                className={tag.title === activeTags.title && !smallScreen ? "activeTag" : "btn"}
                key={tag.id}
                onClick={this.onClickBtn.bind(this, tag)}>
                {tag.title === activeTags.title && smallScreen ? this.activeTag(tag.title) : tag.title}
            </button>
        ));

        return (
            <>
                <div className="tagsSection">
                    {listItem}
                </div>
                <div className="gridTags">
                    {listItem}
                </div>
            </>
        );
    }
}

export default Tags;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.1.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.1.1/umd/react-dom.production.min.js"></script>

谢谢!

山姆

我以某种方式想出了问题所在。

不知何故,我输入了“inlineBlock”而不是“inline-block”。这个错字并没有让样式从无更改为内联块。这是代码:

import React, {Component} from 'react';

class Tags extends Component {
    constructor(props) {
        super(props);
        this.state = {
            smallScreen: window.innerWidth < 800,
            tags: [
                {"id": 1, "title": "World Of Tanks"},
                {"id": 2, "title": "World Of Warplanes"},
                {"id": 3, "title": "World Of Warship"},
            ],
            activeTags: {"id": 2, "title": "World Of Warplanes"},
            displaying: "none",
        }
    }

    // This method will be called each time user clicked on a button and will change the style and state (active tag)
    onClickBtn = (tag) => {
        if (this.state.smallScreen) {
            if (tag.id === this.state.activeTags.id) {
                this.getStyles();
            } else {
                this.getStyles();
                this.setState({activeTags: tag});
            }
        } else {
            this.setState({activeTags: tag});
        }
    };

    // This method will be called from the onClickBtn method for changing the state
    getStyles = () => {
        if (this.state.displaying === "none") {
            this.setState({displaying: "inline-block"})
        } else {
            this.setState({displaying: "none"})
        }
    };

    // This method will add the "->" before the title of active tag in small screen
    activeTag = (title) => "-> " + title;

    render() {
        const {tags, activeTags, smallScreen, displaying} = this.state;

        // 1300 px
        const listItem = tags.map((tag) => (
            <button
                style={tag.title !== activeTags.title && smallScreen ? {display: displaying} : {display: "inline-block"}}
                className={tag.title === activeTags.title && !smallScreen ? "activeTag" : "btn"}
                key={tag.id}
                onClick={this.onClickBtn.bind(this, tag)}>
                {tag.title === activeTags.title && smallScreen ? this.activeTag(tag.title) : tag.title}
            </button>
        ));

        return (
            <>
                <div className="tagsSection"> {/* screens > 800px */}
                    {listItem}
                </div>
                <div className="gridTags"> {/* screens < 800px */}
                    {listItem}
                </div>
            </>
        );
    }
}

export default Tags;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

React-select 在切换时显示键盘

来自分类Dev

在React中在暗模式和亮模式之间切换

来自分类Dev

React Hooks:切换模式下的重新渲染次数过多

来自分类Dev

React.js:如何在使用useContext的身体切换中实现暗/亮模式?

来自分类Dev

在React中切换CSS类

来自分类Dev

在React中切换类

来自分类Dev

在React中切换功能

来自分类Dev

React onClick状态不会切换

来自分类Dev

在React中切换侧边栏

来自分类Dev

如何用react钩子切换

来自分类Dev

在React中切换功能

来自分类Dev

在React中切换CSS类

来自分类Dev

React - 切换 css 类

来自分类Dev

从 React 中的父 onfocus 方法切换显示子组件上的隐藏类

来自分类Dev

React不会切换到生产模式

来自分类Dev

开/关切换以过滤内容React

来自分类Dev

在React中使用钩子切换布尔值

来自分类Dev

无法在React中的onClick上切换类

来自分类Dev

react-router-dom页面不会切换?

来自分类Dev

在React JSX中的div之间切换

来自分类Dev

使用json填充React中的case切换语句

来自分类Dev

调用onClick()时切换按钮值React

来自分类Dev

使用React Router时切换状态

来自分类Dev

React JS切换所选项目

来自分类Dev

如何使用react js实现切换效果?

来自分类Dev

在 React 中单击即可切换状态值

来自分类Dev

React Native 自定义切换按钮组件

来自分类Dev

React.js:单击时切换 className

来自分类Dev

React:为什么元素样式不与 setState 切换?