问题描述
我有一个应用程序,它显示行中的项目列表.每个项目都有一个相应的删除按钮.当我单击它时,它会显示一个带有确认的模式.当我单击取消"时,该操作被取消.当我单击确认"时,模式如下:
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>
这篇关于具有删除功能的反应模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20
前端开发问题
5
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20
前端开发问题
5
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)