简体   繁体   English

React /不可变-基本设置

[英]React/ Immutable - Basic setup

I'm trying to learn/ get my head around immutable so I can set it up with React/ Redux. 我正在尝试学习/让我的头脑变得一成不变,因此可以使用React / Redux进行设置。 Here's my store setup: 这是我的商店设置:

store.js store.js

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
)

combine reducers: 组合减速器:

import { combineReducers } from 'redux';
import { UIreducer } from './UI-reducer';

export default combineReducers({
  UIreducer
});

example reducer: 减速器示例:

import { fromJS } from 'immutable';

const initialState = fromJS({
  test: false
});

export const UIreducer = (state = initialState, action) => {
  switch (action.type) {
    case 'TEST': {
      return state.set('test', true)
    }
    default: return state
  }
}

I'm fairly sure i've set the above two parts up correctly, the only thing that i'm not sure about is how to map the state to props in my component: 我相当确定我已经正确设置了以上两个部分,我不确定的唯一事情就是如何将状态映射到组件中的props:

Here's how I normally do it: 这是我通常的做法:

const mapStateToProps = state => ({
  UI: state.UIreducer
})

When using immutable it returns an object that immutable generates which doesn't look like a normal state object if I were to not use immutable. 当使用不可变时,它返回一个不可变生成的对象,如果我不使用不可变的话,它看起来不像正常状态对象。 I tried after a bit of research to use .get with the state like so: 经过一些研究,我尝试将.get与如下状态一起使用:

const mapStateToProps = state => ({
  UI: state.get('UIreducer')
})

This however returned the error message: 但是,这返回了错误消息:

state.get is not a function state.get不是一个函数

Could someone please point out where i've gone wrong? 有人可以指出我哪里出问题了吗?

You could use redux-immutable and convert the initialState to an immutable object: 您可以使用redux-immutable并将initialState转换为不可变对象:

import {
  combineReducers
 } from 'redux-immutable';

import {
 createStore
} from 'redux';

const initialState = Immutable.Map();
const rootReducer = combineReducers({UIreducer});

Remember the state is the combination of all reducers, so the state will look like the combined reducers object 请记住,状态是所有减速器的组合,因此状态看起来像是组合的减速器对象

export default combineReducers({
  UIreducer
});

At mapStateToProps you should ask for state.UIreducer and at the component level UI.get('test') mapStateToProps您应该询问state.UIreducer以及在组件级别UI.get('test')

Answering further questions: 回答其他问题:

  1. where exactly would I write the UI.get('test') ? UI.get('test')UI.get('test')

Anywhere in your component UI will be a prop for it, you can use it like this 组件用户界面中的任何地方都是它的道具,您可以像这样使用它

const myComponent = (props) => {
 return <div>props.UI.get('test')</div>
}
  1. I would have thought the .get('test') would go in the mapStateToProps somehwere. 我本以为.get('test')会放在mapStateToProps中。

That depends of which properties of your state you want to pass/map to your component, you can also pass an specifict 这取决于要传递/映射到组件的状态的哪个属性,也可以传递特定参数

const mapStateToProps = state => ({
  test: state.UIreducer.get('test')
})

and inside of you component 在你的内部

const myComponent = (props) => {
 return <div>props.test</div>
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM