React-Redux:不能使用商店

内蒂

我是React的新手。我现在正在学习Redux。我了解该库的工作原理,但我完全不理解为什么我的代码不起作用。问题是:React看不到商店我无法使用商店中的数据。

store.js:

   import {createStore} from "redux";
    import reducer from "./reducers/reducer";

    const initialState = {
      notes: [
        {
          name: "First note",
        },
        {
          name: "Second note",
        },
      ]
    };

   const store = createStore(reducer, initialState)
   export default store;

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import store from "./store";
import App from './containers/App.js';

ReactDOM.render(
    (<Provider store={store}>
      <App />,
    </Provider>),
    document.getElementById('root'));

App.js:

import React from 'react';
import MainContainer from "./MainContainer";
// import {connect} from "react-redux";
function App() {
  return (
   <div className="App">
     <MainContainer />
   </div>
  );
}

export default App;

MainContainer.js:

import React from 'react';
import {connect} from 'react-redux'

function MainContainer(props) {
  return (
      <div className="main-container">
        <ul>
          {props.notes.map(note => <li key={note.name}>{note.name}</li>)}
        </ul>
      </div>
  );
}

const mapStateToProps = state => ({
  notes: state.notes
});
export default connect(mapStateToProps)(MainContainer)

reducer.js:

import uuid from 'react-uuid';

function reducer(state, action) {
  if (action.type === 'CREATE_NOTE') {
    return ({
          notes: [
            ...this.state.notes,
            {
              key: uuid(),
              id: uuid(),
              name: action.input,
              detail: action.inputTextArea,
            }
          ]
        }
    )
  }
}

export default reducer;

我尝试使用connect(),但结果相同。

预先感谢您的帮助。

谢谢大家!我在减速机上弄错了。我只是意识到我没有回到状态

隆塞

您提供的代码中似乎缺少存储。尝试这个:

import {createStore} from "redux";
import reducer from "./reducers/reducer";

const initialState = {
   notes: [
      {
         name: "First note",
      },
      {
         name: "Second note",
      },
   ]
};


const store = createStore(
     reducer,
     initialState
)

export default store

现在访问你的店,你可以useSelector或者connectreact-redux图书馆

//在选择器中使用

import { useSelector } from 'react-redux'
const notes = useSelector(state => state.notes)

//使用连接

首先,您需要importeconnectreact-redux这样的:

import {connect} from 'react-redux'

像这样使用

const MyAwesomeComponent = (props)=> (
   props.notes.map(note => <li key={note.name}>{note.name}</li>)
)

const mapStateToProps = (state)=> {
   const { notes } = state
   return notes
}

export default connect(mapStateToProps)(MyAwesomeComponent)

记住要查看react-redux文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我不能使用Redux从React.js中的数组中删除元素

来自分类Dev

React HighCharts不能使用标记群集

来自分类Dev

React Context 不能使用 Provider

来自分类Dev

使用react-router时不能使用多个布局

来自分类Dev

在React中使用Redux商店进行微优化值得吗

来自分类Dev

React-leaflet 不能使用 create-react-app 和 react 16.8.4

来自分类Dev

React / React Native:不能使用以前状态的键来设置状态吗?

来自分类Dev

所有React Native Firebase模块必须具有相同的版本-不能使用Auth

来自分类Dev

react-admin打字稿:不能使用名称空间作为类型

来自分类Dev

我不能使用npm安装任何软件包(需要react-router-dom)

来自分类Dev

React-Native - 我不能使用状态字符串作为新的列表元素

来自分类Dev

使用react-router和redux时如何维护商店的状态?

来自分类Dev

不能使用样式化组件将样式应用于React Native中的自定义组件吗?

来自分类Dev

在React中,为什么我不能使用嵌套的箭头函数...虽然正常的函数似乎可以正常工作?

来自分类Dev

Redux与React-与组件共享商店的正确方法

来自分类Dev

React + Redux:商店更新时如何更新状态

来自分类Dev

Redux 商店更新后,React 组件未收到道具

来自分类Dev

不能使用Runnable

来自分类Dev

Ubuntu,不能使用@

来自分类Dev

不能使用Ubuntu

来自分类Dev

不能使用 QUrl

来自分类Dev

不能使用 FloatingActionButton?

来自分类Dev

Redux:无法使用商店

来自分类Dev

使用Webpack调试Redux / React

来自分类Dev

使用react + redux进行休息

来自分类Dev

未从商店传递Redux-Toolkit状态。react-redux

来自分类Dev

Woocommerce-不能使用“快速订购一页商店”作为客人添加到购物车吗?

来自分类Dev

在带有React和Flux的商店中使用数据集合

来自分类Dev

为什么我不能使用二级用户商店帐户的电子邮件地址登录 wso2 api 商店

Related 相关文章

  1. 1

    为什么我不能使用Redux从React.js中的数组中删除元素

  2. 2

    React HighCharts不能使用标记群集

  3. 3

    React Context 不能使用 Provider

  4. 4

    使用react-router时不能使用多个布局

  5. 5

    在React中使用Redux商店进行微优化值得吗

  6. 6

    React-leaflet 不能使用 create-react-app 和 react 16.8.4

  7. 7

    React / React Native:不能使用以前状态的键来设置状态吗?

  8. 8

    所有React Native Firebase模块必须具有相同的版本-不能使用Auth

  9. 9

    react-admin打字稿:不能使用名称空间作为类型

  10. 10

    我不能使用npm安装任何软件包(需要react-router-dom)

  11. 11

    React-Native - 我不能使用状态字符串作为新的列表元素

  12. 12

    使用react-router和redux时如何维护商店的状态?

  13. 13

    不能使用样式化组件将样式应用于React Native中的自定义组件吗?

  14. 14

    在React中,为什么我不能使用嵌套的箭头函数...虽然正常的函数似乎可以正常工作?

  15. 15

    Redux与React-与组件共享商店的正确方法

  16. 16

    React + Redux:商店更新时如何更新状态

  17. 17

    Redux 商店更新后,React 组件未收到道具

  18. 18

    不能使用Runnable

  19. 19

    Ubuntu,不能使用@

  20. 20

    不能使用Ubuntu

  21. 21

    不能使用 QUrl

  22. 22

    不能使用 FloatingActionButton?

  23. 23

    Redux:无法使用商店

  24. 24

    使用Webpack调试Redux / React

  25. 25

    使用react + redux进行休息

  26. 26

    未从商店传递Redux-Toolkit状态。react-redux

  27. 27

    Woocommerce-不能使用“快速订购一页商店”作为客人添加到购物车吗?

  28. 28

    在带有React和Flux的商店中使用数据集合

  29. 29

    为什么我不能使用二级用户商店帐户的电子邮件地址登录 wso2 api 商店

热门标签

归档