【问题标题】:React Bootstrap Checkbox in Nav -> NavItem does not rerender on clickNav 中的 React Bootstrap Checkbox -> NavItem 不会在单击时重新呈现
【发布时间】:2018-04-17 09:23:17
【问题描述】:

我在使用带有来自 React Bootstrap 的复选框的 Nav 和 NavItem 时遇到了一个特殊的问题。问题是,如果我直接单击复选框而不是 NavItem 按钮,复选框将不会正确重新呈现,但我的状态已更新。

示例:给定下面的代码,我渲染组件并直接单击复选框。在这种情况下,showMap 将设置为 false,因为我们在构造函数中将其设置为 true,但在 html 视图中仍会选中复选框。但是,如果我单击 NavItem 而不是直接单击复选框,则状态 showMap 和视图都会正确更新。

https://react-bootstrap.github.io/components.html#navs

import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinkContainer } from 'react-router-bootstrap';
import { Col, Nav, NavItem, Checkbox } from "react-bootstrap";

interface IProps {

}

interface IState {
    showMap: boolean
}

export class Menu extends React.Component<IProps, IState> {
    constructor(props: any) {
        super(props);
        this.state = {
            showMap: true
        }
    }

    toggleCheckbox = (event: any) => {
        event.preventDefault();
        this.setState({ showMap: !this.state.showMap });
    }

    render() {
        return (
            <div>
                <Nav bsStyle="tabs" activeKey="1">
                    <LinkContainer to="/test">
                        <NavItem eventKey="1">Test</NavItem>
                    </LinkContainer>
                    <LinkContainer to="/test2">
                        <NavItem eventKey="2">Test2</NavItem>
                    </LinkContainer>
                    <NavItem eventKey="3" onClick={this.toggleCheckbox}><Checkbox name="showMap" inline checked={this.state.showMap} readOnly >
                    Show Map </Checkbox></NavItem>
                </Nav>
            </div>
        )
    }
}

更新:

也这样试了,结果还是一样:

import * as React from "react";
import * as ReactDOM from "react-dom";
import { LinkContainer } from 'react-router-bootstrap';
import { Navbar, Nav, NavItem, Checkbox } from "react-bootstrap";

interface IProps {

}

interface IState {
    showMap: boolean
}

export class Menu extends React.Component<IProps, IState> {
    constructor(props: any) {
        super(props);
        this.state = {
            showMap: true
        }
    }

    toggleCheckbox = (event: any) => {
        event.preventDefault();
        this.setState({ showMap: !this.state.showMap });
    }

    render() {
        return (
            <div>
                <Navbar>
                    <Nav bsStyle="tabs" activeKey="1">
                        <LinkContainer to="/test">
                            <NavItem eventKey="1">Test</NavItem>
                        </LinkContainer>
                        <LinkContainer to="/test2">
                            <NavItem eventKey="2">Test2</NavItem>
                        </LinkContainer>
                        <NavItem eventKey="5" onClick={this.toggleCheckbox}><input type="checkbox" name="showMap"
                            checked={this.state.showMap} readOnly />Show map</NavItem>
                    </Nav>
                   </Navbar>
            </div>
        )
    }
}

【问题讨论】:

  • 这可能是因为您没有将 Nav 和 NavItem 组件包含在 react-bootstraps 的 Navbar 组件中。可能值得重构您的代码以使用 NavBar 的 onSelect 处理程序。
  • 还在看这个,但它看起来像是 Checkbox 组件的问题。我在这里查看codesandbox.io/s/3r63j11pzm
  • @BenSmith 实现了导航栏但仍然没有运气,如果我调试代码,我可以看到 onClick 被点击并且复选框在视图中看起来不错。然而,在我的 onClick 代码运行后,它被重置为看起来没有任何变化但状态已更新。即使我正在运行 event.preventDefault(); 也会发生这种情况。
  • @GarryTaylor 尝试使用&lt;input type="checkbox" name="showMap" checked={this.state.showMap} readOnly /&gt;,但行为相同。
  • 我也玩过 @GarryTaylor 不错的代码和框示例,它看起来确实是 react-bootstrap 库的问题。

标签: javascript twitter-bootstrap reactjs typescript react-bootstrap


【解决方案1】:

在此处创建了 React Bootstrap 问题。 https://github.com/react-bootstrap/react-bootstrap/issues/2876

但他们确实认为这与 React 有关,所以我向他们提出了一个问题 https://github.com/facebook/react/issues/11539

然而,React 团队得出的结论是这是浏览器原生行为。

如果有人在这里结束,这就是我修复它的方法:

toggleCheckbox = (event: any) => {
    this.setState({ showMap: !this.state.showMap });
}

const showMapStyle: React.CSSProperties = {
    paddingTop: '15px',
    paddingBottom: '15px',
}

    ...
    </Nav>
    <div onClick={this.toggleCheckbox} role='button' style={showMapStyle}><input type="checkbox" name="showMap"
        checked={this.state.showMap} readOnly />Show map</div>
</Navbar>

我也尝试在&lt;Nav&gt; 中添加一个li 项目,但后来我收到了这个警告:

React 无法识别 DOM 元素上的 activeKey 属性。如果你 故意希望它作为自定义属性出现在 DOM 中, 将其拼写为小写activekey。如果你不小心通过了 从父组件中移除它,从 DOM 元素中移除它。

https://github.com/react-bootstrap/react-bootstrap/issues/2199

<li onClick={this.toggleCheckbox} role='button' style={showMapStyle}><input type="checkbox" id="showMap" name="showMap"
    checked={this.state.showMap} readOnly />Show map</li>

【讨论】:

  • 你最后做了什么?我面临着同样的问题。
  • 我只是将li 放在NavDropdown 中,它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2016-11-09
  • 2019-07-04
  • 2016-06-11
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
  • 2017-06-16
  • 1970-01-01
相关资源
最近更新 更多