javascript - React - 仅切换一个按钮,而不是全部

标签 javascript reactjs dictionary onclick toggle

我有一个从 API 渲染城市的应用程序,每个城市下方应该有一个按钮来显示该城市的描述。现在,当我单击一个按钮时,所有按钮都会更改文本并显示描述组件。

我看到了几个类似的问题,我知道,我可能应该使用索引来告诉哪个按钮应该在单击时起作用,但是我在将其应用到我的代码时遇到了问题。

class CitiesList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            opened: false,
        }
        this.toggleButton = this.toggleButton.bind(this);
    };

    toggleButton() {
        const {opened} = this.state;
        this.setState({
            opened: !opened,
        })
    }


    render() {

        const items = this.props.cities.map((data, index) => (
            <div key={index}>
                <h2>{data.city}</h2>
                <p>PM 2.5 value: {data.value} {data.unit}</p>
                {this.state.opened ? <Button variant="outlined" color="primary" size="small" onClick={this.toggleButton}>Hide city description</Button> : <Button variant="outlined" color="primary" size="small" onClick={this.toggleButton}>Show city description</Button>}
                {this.state.opened && <CityDescription/>}

            </div>
        ));
        return (
            <div>
                <div>
                    {items}
                </div>
            </div>
        )
    }
}

在 CityDescription 组件中现在我只有:

class CityDescription extends React.Component {

    render() {
        return (
            <div>
                description
            </div>
        )
    }
}

预先感谢您的帮助。

最佳答案

正如@Muhammad 所提到的,您只需进行少量更改即可完成这项工作。

下面的代码应该可以工作:

class CitiesList extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            opened: false,
        }
    };

    toggleButton = (index) => {
        const {opened} = this.state;
        this.setState({
            opened: {
              ...opened,
              [index]: !opened[index]
            }
        })
    }


    render() {
        const { opened } = this.state;
        const items = this.props.cities.map((data, index) => (
            <div key={index}>
                <h2>{data.city}</h2>
                <p>PM 2.5 value: {data.value} {data.unit}</p>
                {opened[index] ? <Button variant="outlined" color="primary" size="small" onClick={() => this.toggleButton(index)}>Hide city description</Button> : <Button variant="outlined" color="primary" size="small" onClick={() => this.toggleButton(index)}>Show city description</Button>}
                {opened[index] && <CityDescription/>}

            </div>
        ));
        return (
            <div>
                <div>
                    {items}
                </div>
            </div>
        )
    }
}

关于javascript - React - 仅切换一个按钮,而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59380109/

相关文章:

javascript - react ,Firebase : Access values of JSON and also get key value

javascript - 无法将 ref 分配给渲染 Prop 中的组件

javascript - 如何在没有 Now 的情况下在本地构建 NextJS 项目

python - 需要使用 iLoc 的功能,但对于字典值(文本)

python - 在 for 循环中迭代字典

javascript - 未捕获的类型错误 : Cannot read property 'get' of undefined error

Javascript 奇怪的 Pass-by-X 事件

javascript - Raphael JS 中的动画字体大小是否有流畅的过渡可能性?

javascript - 将文件分配为隐藏输入的值

c++ - 两个 `std::map` 的交集