- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 react 状态的工作内联对话框。工作代码如下。
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import PropTypes from 'prop-types';
import Button from '@atlaskit/button';
import InlineDialog from '@atlaskit/inline-dialog';
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
class ButtonActivatedDialog extends PureComponent {
static propTypes = {
content: PropTypes.node,
position: PropTypes.string,
}
state = {
isOpen: false,
};
handleClick = () => {
this.setState({
isOpen: !this.state.isOpen,
});
}
handleOnClose = (data) => {
this.setState({
isOpen: data.isOpen,
});
}
render() {
return (
<InlineDialog
content={this.props.content}
position={this.props.position}
isOpen={this.state.isOpen}
onClose={this.handleOnClose}
>
<Button
onClick={this.handleClick}
isSelected
>
The Button
</Button>
</InlineDialog>
);
}
}
const App = () => (
<ButtonActivatedDialog
content={
<div>
<h5>
Displaying...
</h5>
<p>
Here is the information I need to display.
</p>
</div>}
position='bottom right'
/>
);
render(<App />, document.getElementById('root'));
我希望按钮具有相同的行为,但使用 redux 来维护对话框的状态。
在阅读了一些 Material 后,我相信我需要发送一个操作来激活一个 reducer 女巫,从而帮助我更新对话框的状态。但是,我不认为我完全理解应该如何将其组合在一起。
这是我正在进行的工作,但由于某种原因,我的 codeSanbox 不喜欢我创建商店的格式。
mport React, { PureComponent } from 'react';
import { render } from 'react-dom';
import PropTypes from 'prop-types';
import Button from '@atlaskit/button';
import InlineDialog from '@atlaskit/inline-dialog';
import { connect, createStore } from 'react-redux'
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
const mapStateToProps = state => {
return {
isDialogOpen: false,
}
}
const mapDispatchToProps = dispatch => {
return {
toggleDialog: () => dispatch({
type: 'TOGGLE_DIALOG'
})
}
}
// action:
const tottleDialog = 'TOGGLE_DIALOG';
//action creator
const toggleDialog = (e) => ({
type: 'TOGGLE_DIALOG',
e,
})
class ButtonActivatedDialog extends PureComponent {
static propTypes = {
content: PropTypes.node,
position: PropTypes.string,
}
state = {
isOpen: false,
};
handleClick = () => {
this.setState({
isOpen: !this.state.isOpen,
});
}
handleOnClose = (data) => {
this.setState({
isOpen: data.isOpen,
});
}
render() {
return (
<InlineDialog
content={this.props.content}
position={this.props.position}
isOpen={this.state.isOpen}
onClose={this.handleOnClose}
>
<Button
onClick={this.handleClick}
isSelected
>
The Button
</Button>
</InlineDialog>
);
}
}
const App = () => (
<ButtonActivatedDialog
content={
<div>
<h5>
Displaying...
</h5>
<p>
Info here
</p>
</div>}
position='bottom right'
/>
);
const store = createStore(toggleDialog, {})
//need and action
//need an action creator - a function that returns an action:
//
// render(<App />, document.getElementById('root'));
render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root')
);
最佳答案
好的,首先你必须设置你的 redux 状态。我们通常根据此处的重新鸭子模式执行此操作:https://github.com/alexnm/re-ducks
这意味着您将为应用程序的每个“部分”创建一个目录。每个部分都有一个:
因此,在您的示例中,我将创建一个 state/inlineMenu 文件夹,并在其中包含以下文件:
actions.js:
import types from './types';
const toggleState = {
type: types.TOGGLE_STATE
};
export default {
updateMenuState
}
操作.js:
import actions from './actions';
const toggleState = actions.toggleState;
export default {
updateMenuState
};
reducers.js:
import types from './types';
const initialState = {
isOpen: false // closed per default
};
const inlineMenuReducer = (state = initialState, action) => {
switch (action.type) {
case types.TOGGLE_STATE:
return { ...state, isOpen: !state.isOpen }
default:
return state;
}
};
export default inlineMenuReducer;
选择器.js:
const isMenuOpen = state => state.inlineMenu.isOpen;
export default {
isMenuOpen
};
类型.js:
const TOGGLE_STATE = 'inlineMenu/TOGGLE_STATE';
export default {
TOGGLE_STATE
};
index.js:
import reducer from './reducers';
export { default as inlineMenuSelectors } from './selectors';
export { default as inlineMenuOperations } from './operations';
export default reducer;
您还必须设置默认提供程序。您可能应该调整选择器中 isOpen 属性的路径。
现在您已经设置了全局 redux 状态。
我们现在需要将其中的数据获取到 View 中。为此,我们需要使用 redux 的 connect 函数,其中它将获取操作和选择器并将它们映射到默认的 React props。
因此您的连接组件可能如下所示:
import React, { PureComponent } from 'react';
import { render } from 'react-dom';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Button from '@myKitkit/button';
import InlineDialog from '@mykit/inline-dialog';
import { inlineMenuOperations, inlineMenuOperations } from '../path/to/state/inlineMenu';
const styles = {
fontFamily: 'sans-serif',
textAlign: 'center',
};
class ButtonActivatedDialog extends PureComponent {
static propTypes = {
content: PropTypes.node,
position: PropTypes.string,
toggleState: PropTypes.func.isRequired
}
handleClick = () => {
const { toggleState } = this.props;
// This will dispatch the TOGGLE_STATE action
toggleState();
}
handleOnClose = () => {
const { toggleState } = this.props;
// This will dispatch the TOGGLE_STATE action
toggleState();
}
render() {
return (
<InlineDialog
content={this.props.content}
position={this.props.position}
isOpen={this.props.isOpen}
onClose={this.handleOnClose}
>
<Button
onClick={this.handleClick}
isSelected
>
The Button
</Button>
</InlineDialog>
);
}
}
// You need to add the provider here, this is described in the redux documentation.
const App = () => (
<ButtonActivatedDialog
content={
<div>
<h5>
Displaying...
</h5>
<p>
Here is the information I need to display.
</p>
</div>}
position='bottom right'
/>
);
const mapStateToProps = state => ({
isOpen: inlineMenuSelectors.isOpen(state);
});
const mapDispatchToProps = dispatch => ({
toggleState: () => dispatch(inlineMenuOperations.toggleState())
}
const ConnectedApp = connect(mapStateToProps, mapDispatchToProps);
render(<ConnectedApp />, document.getElementById('root'));
关于javascript - 如何在 React Redux 应用程序中打开和关闭内联对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47507764/
我一直在尝试将 redux sagas 和 redux 工具包引入我的项目。我目前遇到的问题是 watcher saga 没有捕捉到 takeEvery 中的调度 Action 。效果并运行处理程序。
我需要使用 deep-freeze 库来深度卡住 react redux 应用程序中的整个 redux 状态,以检查状态是否有任何变化。我应该如何使用 deep-freeze 和 React redu
这是一个关于 Redux js 中状态的问题。我在该州有一个数组列表: { list: list } 根据 Redux 文档,我不应该修改 reducer 中的状态。我想在列表中添加一个新项目。我
我正在构建一个应用程序,在用户向下滚动时执行操作。如果我可以在用户再次向上滚动时撤消这些操作,基本上将滚动变成浏览操作时间线的一种方式,那就太好了。 Redux 中是否有内置的方法来做到这一点?或者我
从我从 Dan Abramov 的蛋头视频“javascript-redux-colocating-selectors-with-reducers”和他的一些推文中了解到,使用选择器将状态映射到 Pr
尝试使用 redux saga 运行 reduxdevtools: 收到此错误: Error Before running a Saga, you must mount the Saga middle
Redux 工具包文档提到在多个 reducer 中使用操作(或者更确切地说是操作类型) First, Redux action types are not meant to be exclusive
Redux 将调度状态更改的操作。 redux 中 Action 类型的命名约定是什么? 最佳答案 社区中有一些约定,我将在这里列出我知道并认为有用的约定: 最常见的约定是 将 Action 类型(“
使用 Redux Toolkit 可以吗,即使我只在其中创建 Slice 并通过 Redux Saga 解决中间件问题? 或者在这种情况下,最佳实践是使用 Redux Saga + raw Redux
Redux 如何处理深度嵌套的叶子模型变更?意思是,我正在从叶子一直发送到它的 reducer 句柄的更改事件,并且我不想将更改事件广播到整个树。 最佳答案 在 Redux 中,所有操作总是被分派(d
redux 使用什么类型的数据结构来使数据在 Angular 和 React.js 中持久化?我假设它使用持久数据结构。 最佳答案 Redux 是一种用于管理状态的架构。它不使用任何数据结构。它保留您
我们正在计划一个 Electron 应用程序,并且我们正在考虑 Redux。该应用程序将具有巨大 状态,可能会从数十个或数百个文件中读取数据。在做一些了解 Redux 的研究时,我发现 reducer
我不想添加属性 sections: []到我的对象 formOpen在 reducer 中,我收到我的对象 formOpen从我的服务器和其他属性,我想添加这个,我该怎么做? 谢谢 import {
我使用 redux-saga 的主要原因之一是它进行异步函数调用的可测试性。我的困境是,当我使用不属于我的 redux 存储的有状态对象进行编程时,使用 sagas 进行编程变得非常尴尬。是否有使用非
我是 redux 的新手,所以我有几个问题希望得到解答。如果您能解释一些有关构建 redux 架构的内容,那就太好了。 此时我使用 Flutter_Redux 包 ( https://pub.dart
我正在使用 React + Flux。我们的团队正计划从 flux 转向 redux。来自 Flux 世界的我对 Redux 感到非常困惑。在 flux 控制流中很简单,从组件 -> 操作 -> 存储
这个问题与过去不同,这就是为什么。这个问题是什么时候。由于两者本身都是很好的框架,所以问题是我什么时候应该使用 thunk 而不是 saga。因为我的一位 friend 一直坚持让我在我们的应用程序中
我搜索了高低,但找不到明确的答案。 我已经设法绕开 Redux 的机制,但是当我谈到 API 调用和异步操作创建者时,我被 Promises 上下文中的中间件所困。 你能帮我把乱七八糟的东西弄好吗?
我正在使用 redux-saga 但遇到了一个问题:redux-auth-wrapper 需要 redux-thunk进行重定向,所以我只是在我的商店中添加了 thunk: import {creat
问题(tl;博士) 我们如何创建 custom redux-orm reducer与 redux-toolkit的createSlice ? 有没有比这个问题中提供的尝试更简单、推荐、更优雅或只是其他
我是一名优秀的程序员,十分优秀!