【发布时间】:2018-09-05 19:16:51
【问题描述】:
我正在做这个项目,其中前端在 React 中与 UIkit 一起用于用户界面。各部分之间的集成看起来执行得很差。我将解释为什么。有一个Modal 组件,类似于
export class Modal extends Component {
static getByName = name => UIkit.modal(`[data-modal-name='${name}']`)
static show = name => {
const modal = Modal.getByName(name)
if (modal) modal.show()
}
static hide = name => {
const modal = Modal.getByName(name)
if (modal) modal.hide()
}
render() {
// a modal
}
}
这是这样使用的
export const LoginFormModal = props => (
<Modal name="login-form" className="login-form-modal" hideClose>
<LoginForm />
</Modal>
)
并在需要时以编程方式调用显示/隐藏(甚至是 redux 的操作)
Modal.hide("login-form")
这是在 Redux 操作中,像这样
export const login = credentials => {
return dispatch => {
dispatch(showLoader())
API.authentication.login(
credentials,
response => {
setCurrentUser(
Object.assign({}, response.user, { user_id: response.user.id })
)
Modal.hide("login-form")
dispatch(loginSucceded(response))
dispatch(hideLoader())
dispatch(push("/"))
dispatch(fetchNotificationsCounter())
},
error => {
dispatch(loginFailed(error))
dispatch(hideLoader())
}
)
}
}
这似乎有效。直到你离开一个组件。当你回到它时,第二次以编程方式隐藏不再起作用。
任何人都可以指导我如何以更适合反应的方式集成这些部分?
【问题讨论】:
-
redux state 可以用这个吗?你能在你的 redux 操作中添加代码示例吗?
-
添加了一个redux动作作为例子
-
它第一次工作,我的模态以编程方式关闭。但是,如果我离开组件(卸载)并返回它,就再也不会工作了。
-
您必须获得表明您已在
LoginFormModal登录的状态