简体   繁体   English

如何根据Redux存储上的属性显示/隐藏组件?

[英]How can I display/hide a component depending on a property on the Redux store?

I am studying Redux but I'm having problems to understand it properly. 我正在研究Redux,但是在正确理解它方面遇到了问题。 Now I am building a personal project with it to learn it. 现在,我正在构建一个个人项目来学习它。

I have this "Settings button" (actually, it is a list item) on the navbar. 我在导航栏上有这个“设置按钮”(实际上是一个列表项)。 When I click it, I want that another component shows itself. 当我单击它时,我希望另一个组件显示自己。 This will be the <Settings /> component. 这将是<Settings />组件。 This component has a "Close button" to hide. 该组件具有要隐藏的“关闭按钮”。

My idea is to have a property on the redux global store like this: settingsOpen: false and when I click on the button it becomes true and the component appears and vice-versa. 我的想法是在redux全局存储上具有如下属性: settingsOpen: false ,当我单击按钮时,它变为true,并且该组件出现,反之亦然。 The close button always set the property equal to false and close the component. 关闭按钮始终将属性设置为false并关闭组件。

Here you can find all the code: https://github.com/lbluigi/react-ecommerce 在这里您可以找到所有代码: https : //github.com/lbluigi/react-ecommerce

Anyway, I think the important parts are: 无论如何,我认为重要的部分是:

Header.js component Header.js组件

This is the list item clicked that would toggle the Settings component. 单击的列表项将切换“设置”组件。

<a onClick={(event) => this.props.toggleSettings(event)} href="#settings" className="nav-link"><i className="fa fa-cog" aria-hidden="true"></i> Settings</a>

Settings.js component Settings.js组件

This is the component that had to appear and disappear depending on the settingsOpens property. 这是必须显示和消失的组件,具体取决于settingsOpens属性。

<div className={this.props.settings.settingsOpen ? 'settings' : 'hidden'}>
  <i className="fa fa-times fa-3x" aria-hidden="true"></i>
  {/* just a test <h2>{this.props.settings.themeColor}</h2> */}
</div>

toggleSettings.js action This is the function triggered by the click event on the settings button that I wrote before. toggleSettings.js操作这是我之前编写的设置按钮上的click事件触发的功能。

event.preventDefault();
return {
  type: 'SETTINGS_TOGGLED'
}

settingsReducer.js reducer settingsReducer.js减速器

This is the reducer that sets the initial properties that the user can change interacting with the Settings component. 这是化简器,用于设置用户可以更改的初始属性,以与“设置”组件交互。

{
  settingsOpen: false,
  themeColor: 'light',
  gridView: 'grid'
}

I could write a lot more, but you will find everything on the GitHub repo that I linked before. 我可以写很多东西,但是您会在我之前链接的GitHub存储库中找到所有内容。 I don't know how to proceed to change the property settingsOpen on click. 我不知道如何继续更改属性settingsOpen

Your settingsReducer must be a function which returns an object (state) by dispatching actions. 您的settingsReducer必须是通过分派操作返回对象(状态)的函数。 In your case it should be: 在您的情况下,应为:

const defaultSettings = {settingsOpen: false, themeColor: 'light'}
const settingsReducer = (state = defaultSettings, action) {
  if (action.type === 'SETTINGS_TOGGLED') {
    return {
      ...state,
      settingsOpen: !state.settingsOpen
    }
  }
  return state
}

I created a small example: https://codesandbox.io/s/wq24zkkrvw 我创建了一个小示例: https : //codesandbox.io/s/wq24zkkrvw

Basically I initialised the store with a isOpen=false and every time you click on the button toggles the value of this. 基本上,我使用isOpen = false初始化商店,并且每次您单击按钮时都会切换此值。

reducer: 减速器:

const initState = {
  isOpen: false
};
export default (state = initState, action) => {
  switch (action.type) {
    case 'TOGGLE':
      return { isOpen: !state.isOpen };
    default:
      return state;
  }
};

and this is the component that will dispatch the action: 这是将分派动作的组件:

<Counter
      isOpen={store.getState().isOpen}
      onToggle={() => store.dispatch({ type: 'TOGGLE' })}
    />

If you want to change some store property over time - you'll need a reducer. 如果您想随时间更改某些商店属性-您将需要一个减速器。 Because reducer is function that responsible for store mutations (changes). 因为reducer是负责存储突变(更改)的功能。 You already have one but it always return same value. 您已经有一个,但是它总是返回相同的值。

function settingsReducer() {
  return {
    settingsOpen: false,
    color: 'light',
    view: 'grid'
  }
}

Firstly you'll have to modify it in order to add reaction - once action appear, i would want to modify store like this: , 首先,您必须对其进行修改以增加反应- once action appear, i would want to modify store like this:

function settingsReducer(state, action) {
    if (action.type === 'SETTINGS_TOGGLED') {
      return {
        settingsOpen: !state.settingsOpen,
        color: 'light',
        view: 'grid',
      };
    }
    return {
      settingsOpen: false,
      color: 'light',
      view: 'grid',
    };
  }

At that point you would have to create(define) an action . 此时,您将必须创建(定义)一个action

After that proceed to a link in your header and add dispatch of an action instead returning value. 之后,转到标题中的链接,并添加动作的分派 ,而不是返回值。 Note that you have to connect header component for dispatching. 请注意,必须连接 header组件才能进行分派。 Now your action will appear inside reducer and would be able to o modify store You can use some kind of a logger to check what is happening in your system After you make sure state is changing as you planned - you'd need to connect a Settings component to a store to pass settingsOpen property from store to component. 现在,您的操作将出现在reducer中,并且将能够修改存储。您可以使用某种记录器来检查系统中正在发生的事情确保状态按计划进行了更改之后-您需要连接 Settings组件到商店以将settingsOpen属性从商店传递到组件。

Thats where you return to you Settings component again, but from different side. 那就是您从另一侧再次返回到“设置”组件的位置。 Thinking in redux is always like this - you have to plan and implement whole loop. 在redux中思考总是这样-您必须计划和实施整个循环。 Somethink like: component handles click -> action -> reducer -> store changes -> component -> render changes 像这样: component handles click -> action -> reducer > store changes -> component -> render changes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据Redux操作结果更新React组件 - How can I update a React component depending on redux action outcome 如何通过 Vuex 存储操作更改组件中属性的值? - How Can I change the value of a property in a component by a Vuex store action? 如何从组件外部从Redux存储中获取值? - How can I grab a value from a Redux store from outside of a component? 如何在页面加载时保留我的 redux (redux-saga) 数据而不显示/存储在本地或 session 存储中? - How can I preserve my redux (redux-saga) data without display/store in local or session storage on page load? 如何创建一个在Redux商店更新时更新的功能组件? - How can you create a functional component that updates on Redux store change? 如何在 Redux Store 中按 id 存储项目 - How can I store items by id in Redux Store 如何更改 Lit-Element 中的 Boolean 属性以隐藏组件上的内容? - How can I change a Boolean property in Lit-Element to hide content on my component? 如何使用 JSF 显示/隐藏组件? - How can I show/hide component with JSF? 如何根据下拉选择显示/隐藏div? - How can I show/hide divs depending on dropdown selection? 根据分配给用户角色的属性隐藏/显示元素 - Hide/Display an element depending on a property assigned to a user role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM