首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在redux应用程序中打开和关闭内联对话框

如何在redux应用程序中打开和关闭内联对话框
EN

Stack Overflow用户
提问于 2017-11-27 09:37:15
回答 1查看 1.3K关注 0票数 0

我有一个使用react状态的内联对话框。工作代码如下。

代码语言:javascript
运行
复制
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来维护对话框的状态。

在阅读了一些材料后,我相信我需要派遣一个动作来激活减速机,女巫反过来会帮助我更新对话框的状态。然而,我不认为我完全理解该如何将这两者结合起来。

这是我正在进行的工作,但出于某种原因,我的codeSanbox不喜欢我创建商店的格式。

代码语言:javascript
运行
复制
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')
);

EN

回答 1

Stack Overflow用户

发布于 2017-11-27 11:01:27

好的,首先你必须设置你的redux状态。我们通常按照这里的再鸭子模式来做:https://github.com/alexnm/re-ducks

这意味着您将为应用程序的每个“部分”创建一个目录。然后每个部分都有一个:

  • 操作:在状态上执行任务(例如打开或关闭内联菜单)
  • 选择器:获取状态的一些值(比如内联菜单打开了吗?)
  • 操作:对状态执行操作(如将isOpen设置为true/false)
  • 减速机:将某一动作应用于状态(如上面的动作)
  • 类型:任何类型的状态更改。任何动作都有一个类型,类型决定减速器中的哪个部分被执行。

因此,在您的示例中,我将创建一个state/inlineMenu文件夹,其中包含以下文件:

actions.js:

代码语言:javascript
运行
复制
import types from './types';

const toggleState = {
  type: types.TOGGLE_STATE
};

export default {
  updateMenuState
}

operations.js:

代码语言:javascript
运行
复制
import actions from './actions';

const toggleState = actions.toggleState;

export default {
  updateMenuState
};

reducers.js:

代码语言:javascript
运行
复制
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;

selectors.js:

代码语言:javascript
运行
复制
const isMenuOpen = state => state.inlineMenu.isOpen;

export default {
  isMenuOpen
};

types.js:

代码语言:javascript
运行
复制
const TOGGLE_STATE = 'inlineMenu/TOGGLE_STATE';

export default {
  TOGGLE_STATE
};

index.js:

代码语言:javascript
运行
复制
import reducer from './reducers';

export { default as inlineMenuSelectors } from './selectors';
export { default as inlineMenuOperations } from './operations';

export default reducer;

您还必须设置默认提供程序。应该调整选择器中的isOpen属性的路径。

现在您已经设置了全局redux状态。

我们现在需要把它中的数据放到视图中。为此,我们需要使用redux的连接函数,在该函数中,它将接受操作和选择器,并将它们映射到默认的响应道具。

因此,连接的组件可能如下所示:

代码语言:javascript
运行
复制
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'));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47507764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档