<tfoot id='tjZob'></tfoot>

  • <i id='tjZob'><tr id='tjZob'><dt id='tjZob'><q id='tjZob'><span id='tjZob'><b id='tjZob'><form id='tjZob'><ins id='tjZob'></ins><ul id='tjZob'></ul><sub id='tjZob'></sub></form><legend id='tjZob'></legend><bdo id='tjZob'><pre id='tjZob'><center id='tjZob'></center></pre></bdo></b><th id='tjZob'></th></span></q></dt></tr></i><div id='tjZob'><tfoot id='tjZob'></tfoot><dl id='tjZob'><fieldset id='tjZob'></fieldset></dl></div>

    <small id='tjZob'></small><noframes id='tjZob'>

        • <bdo id='tjZob'></bdo><ul id='tjZob'></ul>

      1. <legend id='tjZob'><style id='tjZob'><dir id='tjZob'><q id='tjZob'></q></dir></style></legend>

        具有删除功能的反应模式

        时间:2023-09-05

        <small id='4YJkB'></small><noframes id='4YJkB'>

          <i id='4YJkB'><tr id='4YJkB'><dt id='4YJkB'><q id='4YJkB'><span id='4YJkB'><b id='4YJkB'><form id='4YJkB'><ins id='4YJkB'></ins><ul id='4YJkB'></ul><sub id='4YJkB'></sub></form><legend id='4YJkB'></legend><bdo id='4YJkB'><pre id='4YJkB'><center id='4YJkB'></center></pre></bdo></b><th id='4YJkB'></th></span></q></dt></tr></i><div id='4YJkB'><tfoot id='4YJkB'></tfoot><dl id='4YJkB'><fieldset id='4YJkB'></fieldset></dl></div>
        1. <tfoot id='4YJkB'></tfoot>
            <tbody id='4YJkB'></tbody>

              • <legend id='4YJkB'><style id='4YJkB'><dir id='4YJkB'><q id='4YJkB'></q></dir></style></legend>
                  <bdo id='4YJkB'></bdo><ul id='4YJkB'></ul>

                  本文介绍了具有删除功能的反应模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个应用程序,它显示行中的项目列表.每个项目都有一个相应的删除按钮.当我单击它时,它会显示一个带有确认的模式.当我单击取消"时,该操作被取消.当我单击确认"时,模式如下:
                  1. 它显示一个加载图标
                  2. 它改变了样式(颜色和文字 - 现在它说它完成了)
                  3.它删除项目

                  到目前为止,我得到的只是显示模式,与本文相同:
                  https://daveceddia.com/open-modal-in-react/
                  加上单击确认时的控制台日志记录.
                  如何实现该功能?
                  我需要使用 Redux 吗?或者是否有某种用于确认对话框的库?

                  我的代码:https://codesandbox.io/s/6n03myqw8w

                  解决方案

                  这是一个没有使用 Redux 的例子.它展示了如何使用组件来解决必须依赖数据存储的问题.主容器保存数据和动作,我们只需传递这些动作和必要的数据即可删除和显示模式.

                  现在这不考虑加载和样式更改,但您可以将它们绑定到 MockModal 类中的操作以添加假加载图标(因为我们使用来自容器的数据,所以数据是即时的).样式更改可以添加到 MockModal 中的 removeTask 方法中,或者您可以研究一种动画技术来增强 UI.以下纯属函数式实现.

                  如果您需要更多帮助,请告诉我.

                  class MockModal extends React.Component {移除任务 = () =>this.props.removeTask(this.props.data);closeModal = () =>this.props.closeModal();使成为() {常量 { id, name } = this.props.data;如果(this.props.displayModal){返回 (

                  <h5>你想删除任务 {id} : {name}</h5><button onClick={this.removeTask}>确认</button><button onClick={this.closeModal}>关闭</button></div>)}返回空值;}}类任务扩展 React.Component {showModal = (任务) =>this.props.show(this.props.task);使成为() {const { id, name } = this.props.task;返回 (

                  <h5>{id}:{name}</h5><button onClick={this.showModal}>(删除)</button></div>)}}类 App 扩展 React.Component {状态 = {显示模式:假,模态:{身份证:空,名称:空,},任务: [{ id: 1, name: '星球大战' },{ id: 2, name: '哈利波特' },{ id: 3, name: '指环王' },],}showModal = (任务) =>this.setState({ modal: task, showModal: true, });hideModal = () =>this.setState({ showModal: false, });removeTask = (activeTask) =>{常量索引 = this.state.tasks.findIndex(任务 => {返回 task.id === activeTask.id;});这个.setState({显示模式:假,任务: [...this.state.tasks.slice(0, index),...this.state.tasks.slice(index + 1),]})}使成为(){返回 (

                  <模拟模态displayModal={this.state.showModal}closeModal={this.hideModal}removeTask={this.removeTask}数据={this.state.modal}/>{this.state.tasks.map(task => {返回 (<任务键={task.id} 任务={task} 显示={this.showModal}/>)})}</div>)}}ReactDOM.render(<App/>, document.getElementById('root'));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></脚本><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script><div id="root"></div>

                  I have an app which shows a list of items in rows. Every item has a corresponding delete button. When I click it, it shows a modal with confirmation. When I click 'cancel', the action is canceled. When I click 'confirm' the modal do as follows:
                  1. It displays a loading icon
                  2. It changes the style (color and text - now it says it is done)
                  3. It removes the item

                  What I get so far is just displaying modal, same as in this article:
                  https://daveceddia.com/open-modal-in-react/
                  Plus just console logging when confirm is clicked.
                  How do I implement the feature?
                  Do I need to use Redux? Or is there some kind of library for confirmation dialogs?

                  My code: https://codesandbox.io/s/6n03myqw8w

                  解决方案

                  Here's an example without using Redux. It shows how to use components to get around the issue of having to rely on a data store. The main container holds the data and actions and we simply pass these actions and the necessary data to remove and display a modal.

                  Now this doesn't account for the loading and style changes but you can bind these to the actions within the MockModal class to add a fake loading icon (since we're using data from a container, the data is instant). The style change could be added to the removeTask method in MockModal, or you can look into an animation technique to enhance the UI. The below is purely a functional implementation.

                  Let me know if you need more help.

                  class MockModal extends React.Component {
                    removeTask = () => this.props.removeTask(this.props.data);
                    closeModal = () => this.props.closeModal();
                    render() {
                      const { id, name } = this.props.data;
                      if (this.props.displayModal) {
                        return (
                          <div>
                            <h5>You want to delete task {id} : {name}</h5>
                            <button onClick={this.removeTask}>Confirm</button>
                            <button onClick={this.closeModal}>Close</button>
                          </div>
                        )
                      }
                      return null;
                    }
                  }
                  
                  class Task extends React.Component {
                    showModal = (task) => this.props.show(this.props.task);
                    render() {
                      const { id, name } = this.props.task;
                      return (
                        <div>
                          <h5>{id}:{name}</h5>
                          <button onClick={this.showModal}>(REMOVE)</button>
                        </div>
                      )
                    }
                  }
                  
                  class App extends React.Component {
                    state = {
                      showModal: false,
                      modal: {
                        id: null,
                        name: null,
                      },
                      tasks: [
                        { id: 1, name: 'Star Wars' },
                        { id: 2, name: 'Harry Potter' },
                        { id: 3, name: 'Lord of the Rings' },
                      ],
                    }
                    showModal = (task) => this.setState({ modal: task, showModal: true, });
                    hideModal = () => this.setState({ showModal: false, });
                    removeTask = (activeTask) => {
                      const index = this.state.tasks.findIndex(task => {
                        return task.id === activeTask.id;
                      });
                      this.setState({
                        showModal: false,
                        tasks: [
                          ...this.state.tasks.slice(0, index),
                          ...this.state.tasks.slice(index + 1),
                        ]
                      })
                    }
                    render(){
                      return (
                        <div>
                          <MockModal 
                            displayModal={this.state.showModal}
                            closeModal={this.hideModal}          
                            removeTask={this.removeTask}
                            data={this.state.modal} 
                          />
                          {this.state.tasks.map(task => {
                            return (
                              <Task key={task.id} task={task} show={this.showModal} />
                            )
                          })}
                        </div>
                      )
                    }
                  }
                  
                  ReactDOM.render(<App />, document.getElementById('root'));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
                  <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
                  <div id="root"></div>

                  这篇关于具有删除功能的反应模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在页面刷新后更改默认 iframe src onclick 并保留 src? 下一篇:我如何找到最后一个&lt;li&gt;&lt;/li&gt;在每个 &lt;ul

                  相关文章

                    1. <small id='ouzyr'></small><noframes id='ouzyr'>

                      <tfoot id='ouzyr'></tfoot>
                      • <bdo id='ouzyr'></bdo><ul id='ouzyr'></ul>

                      <i id='ouzyr'><tr id='ouzyr'><dt id='ouzyr'><q id='ouzyr'><span id='ouzyr'><b id='ouzyr'><form id='ouzyr'><ins id='ouzyr'></ins><ul id='ouzyr'></ul><sub id='ouzyr'></sub></form><legend id='ouzyr'></legend><bdo id='ouzyr'><pre id='ouzyr'><center id='ouzyr'></center></pre></bdo></b><th id='ouzyr'></th></span></q></dt></tr></i><div id='ouzyr'><tfoot id='ouzyr'></tfoot><dl id='ouzyr'><fieldset id='ouzyr'></fieldset></dl></div>

                      <legend id='ouzyr'><style id='ouzyr'><dir id='ouzyr'><q id='ouzyr'></q></dir></style></legend>