简体   繁体   English

是否可以从 Redux 存储中检索特定评论评论的状态

[英]is it possible to retrieve state from Redux store for a particular review comments

i do have a react class component with state in it.我确实有一个带有状态的反应类组件。

class NewComponent extends Component {
  state = {
    modalIsOpen: false,
    aa: true,
    bb: false,
    cc: false,
    dd: false,
    ee: 1,
    dd: [],
    cc: [],
    ff: [],
    gg: [],
    vv: [],
    bb: '',
    rr: '',
    bb: '',
    axcx: 'recent',
    zxc: null,
    asd: [],
    qwe: '',
    asd: '',
    xc: false,
    wxc: false,
    zxcc: null,
    zxcxzc: [],
    zxc: {},
    zxc: false,
    zxc: [],
    zxc: null,
    zxc: '',
    imgGroup: aa.imgGroup,
    videoGroup: xx.videoGroup,
    docGroup: vv.docGroup,
  }
}

is it possible to store these state somewhere in store and import it to this component.if so can you guys please help me to do so.是否可以将这些状态存储在商店的某个地方并将其导入到此组件中。如果可以,请帮助我这样做。 am new to redux.我是 redux 的新手。

Any help will be highly helpful任何帮助都会非常有帮助

This could be done easily.这很容易做到。 What you have to do is add those variables in your reducer to a respective store (let call the store as youstore ).您需要做的是将减速器中的这些变量添加到相应的商店(让商店称为youstore )。

Reducer.js减速器.js

const initialState = {
    modalIsOpen: false,
    aa: true,
    bb: false,
    cc: false,
    dd: false,
    ee: 1,
    dd: [],
    cc: [],
    ff: [],
    gg: [],
    vv: [],
    bb: '',
    rr: '',
    bb: '',
    axcx: 'recent',
    zxc: null,
    asd: [],
    qwe: '',
    asd: '',
    xc: false,
    wxc: false,
    zxcc: null,
    zxcxzc: [],
    zxc: {},
    zxc: false,
    zxc: [],
    zxc: null,
    zxc: ''
}

export default (state = initialState, action) => {
  switch (action.type) {
    case SOME_ACTION:
    ......
}

Now, this should be available from your component as props.现在,这应该可以作为道具从您的组件中获得。 For this should use mapStateToProps method.为此应该使用mapStateToProps方法。

Your_component.jsx your_component.jsx

const mapStateToProps = state => {
  return {
        modalIsOpen: state.youstore.modalIsOpen,
        aa: state.youstore.aa,
        bb: state.youstore.bb,
        cc: state.youstore.cc,
        dd: state.youstore.dd,
        .......
  };
};

const mapDispatchToProps = dispatch => {
  return {
    someMethod: data => dispatch(somemethod(data)),
  };
};

export default connect(
    mapStateToProps,
    mapDispatchToProps
  )(your_Component);

To change the value of a state you have to dispatch an action through mapDispatchToProps要更改状态的值,您必须通过mapDispatchToProps调度操作

You should not have the state inside the component but if you want to it you need to you need to have2 things a action and a reducer.你不应该在组件内部拥有状态,但是如果你想要它,你需要有两个东西,一个动作和一个减速器。 In the reducer, you just have set the state在减速器中,您刚刚设置了状态

dispatch(createState(state))

you need to add that in your component but I would suggest highly against it.你需要在你的组件中添加它,但我强烈建议不要这样做。 SO the right way to do it create you reducer.js and ad this state there.所以正确的方法是创建你的 reducer.js 并在那里广告这个状态。

  const myReducer = (initialState = state, action) => {
  switch (action.type) {
   ....
  }
};

so when you create your reducer just pass the initial state there.所以当你创建你的减速器时,只需在那里传递初始状态。 I hope that helps!我希望这有帮助!

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

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