Reactstrap:让 Modal 发挥作用

问题描述 投票:0回答:3

尝试学习 React 和 Reactstrap 并尝试让 Modals 工作。当我单击按钮时,它应该切换模态,但现在它给我这个错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义。检查“App”的渲染方法

无法弄清楚我做错了什么。有人可以帮我知道我哪里出了问题吗?感谢您提供的任何帮助。

如果可能的话,我想使用最新版本的 React 和 Reactstrap。

这是 Codepen 上的链接:https://codepen.io/lieberscott/pen/ddYNVP

const { Button,
       Container,
       Modal,
       ModalTitle,
       ModalHeader,
       ModalBody,
       ModalFooter } = Reactstrap;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      showModal: false
    }
    this.toggleModal = this.toggleModal.bind(this);
  }

  toggleModal() {
    console.log("hello");
    this.setState({
      showModal: !this.state.showModal
    })
  }

  render() {
    return (
      <Container>
        <Headline />
        <Box />
        <Button outline color="primary" onClick={this.toggleModal}>Click</Button>
        <Modal isOpen={this.state.showModal} toggle={this.toggleModal} className="modal">
          <ModalHeader>
            <ModalTitle id="modalTitle">
              Add a Recipe
            </ModalTitle>
          </ModalHeader>
          <ModalBody>
            Modal body
          </ModalBody>
          <ModalFooter>
            Modal footer
          </ModalFooter>
        </Modal>
      </Container>
    );
  }
}

class Headline extends React.Component {
  render() {
    return (
      <div>
        Recipes
      </div>
    );
  }
}

class Box extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      items: [
        {
        name: "Tofu tacos",
        ingredients: [
          "Shells",
          "lettuce",
          "tofu",
          "paprika"
        ]
      },
      {
        name: "Spaghetti",
        ingredients: [
          "pasta",
          "sauce",
          "salt"
        ]
      }
      ] // end of items
    } // end of this.state
  } // end of constructor
  render() {
    const allitems = this.state.items.map(item => {
      return (
        <div>
          {item.name}
        </div>
      );
    })
    return (
      <div>
        {allitems}
      </div>
    );
  }
}

const app = document.getElementById("app");

ReactDOM.render(<App />, app);
javascript reactjs reactstrap
3个回答
0
投票

我不知道如何像你一样通过 codepen 分享我的代码,抱歉。

我做了这些改变:

toggleModal() {
    console.log("hello");
    console.log( 'before setState: ', this.state );
    this.setState({
      showModal: !this.state.showModal
    })
    console.log( 'after setState: ', this.state );
  }

Button onClick 事件,当您执行

onClik={this.something}
时,此
this
指的是 Button 元素。我改为:

<Button outline color="primary" onClick={() => this.toggleModal()}>Click</Button>

当我这样做时

onClick={ () => this.something() }
允许我利用我的班级的方法。

只需查看 console.log 输出,您就会看到单击按钮将 showModal 更改为

true
false

看看头颅化的答案:https://forum.freecodecamp.org/t/difference-in-reactjs-onclick-function-binding/116027/2


-1
投票

这样就容易多了

在构造函数示例中添加您的状态

 this.state={showLogOut: false}

在您的返回中,您需要的只是 setState true 示例

<Button onClick={() => this.setState({showLogOut: true})}>Show modal</Button>

现在我们需要的是模态代码,因为我们有一个状态和一个 setState

<Modal isOpen={this.state.showLogOut} fade="false"  toggle={() => this.setState({showLogOut: false})}>
<ModalHeader toggle={() => this.setState({showLogOut: false})}>Ready to leave?</ModalHeader>
    <ModalBody>   
        <p>Press logout to end session.</p>                     
    </ModalBody>
<ModalFooter>

    <Button onClick={() => this.setState({showLogOut: false})}>Cancel</Button>
    <Button color="info">Logout</Button>
</ModalFooter>

这应该足以显示您的模式:我假设您正在使用react-strap并且您正在导入所需的元素,但只是为了确保这里是您需要导入的内容

import { Button, Modal, ModalBody, ModalFooter, ModalHeader} from 'reactstrap';

-2
投票

Reactstrap Modal 不显示,因为需要将淡入淡出设置为 False。请看看我的代码谁有效。

class Demoextends extends React.Component {
constructor(props) {
    super(props);
    this.state = {
        modal: false,
        fade: false
    };
    this.toggle = this.toggle.bind(this);
};
toggle() {
    console.log("hello");
    this.setState({
        modal: !this.state.modal
    });
    console.log( 'after setState: ', this.state );
}
render() {
    return (
        <div>
            <Button color="danger" onClick={this.toggle}>Launch</Button>
            <Modal isOpen={this.state.modal} fade={this.state.fade }  toggle={this.toggle}>
                <ModalHeader toggle={this.toggle}>Modal title</ModalHeader>
                <ModalBody>                        
                </ModalBody>
                <ModalFooter>
                    <Button onClick={this.toggle}>Do Something</Button>{' '}
                    <Button onClick={this.toggle}>Cancel</Button>
                </ModalFooter>
            </Modal>
        </div>
    );
}

}

导出默认的Demoextends;

当然你必须在代码中添加reactstrap的一部分

© www.soinside.com 2019 - 2024. All rights reserved.