reactjs 无法在react easy-peasy中使用“useStoreState”访问状态

xxls0lw8  于 2023-02-18  发布在  React
关注(0)|答案(2)|浏览(133)

我最近决定用easy-peasy学习状态管理,并沿着基本教程,但我似乎无法访问状态。

import model from './model';   
import Todo from './components/Todo.tsx';  
import { StoreProvider, createStore } from 'easy-peasy';

const store = createStore(model);  
function App() { 
  return (
    <StoreProvider store={store}>
    <div className="App">
      <Todo />
      </div>
    </StoreProvider>
 );
}

export default App;

下面是模型文件“model.js”

export default {
    todos: [
        {
            id: 1
        },
        {
            id: 2
        },
        {
            id: 3
        }
    ]
};

这是Todo文件:

import React from 'react';
import {useStoreState } from 'easy-peasy';

const Todo = () => {
   //The line below does not work for me, when i do 'state.todos' i get an error that todos does not exist on type 
    const todos = useStoreState(state=>state.todos);
    
    return (
        <div>
            
      </div>  
    );
}

export default Todo;
kx1ctssn

kx1ctssn1#

尝试删除.todos,以便

const todos = useStoreState(state=>state.todos);

变成:

const todos = useStoreState(state=>state);
czfnxgou

czfnxgou2#

从“react”导入React从“easy-peasy”导入{ useStoreState };
从'./Feed'导入源const主页=({正在加载,获取错误})=〉{ const {搜索结果} = useStoreState((状态)=〉状态.搜索结果)return({正在加载帖子...
}; {获取错误&&〈p类别名称=“状态消息”样式={{颜色:“红色”}}〉{获取错误}
}; {!正在加载&&!提取错误&&(搜索结果长度?:没有要显示的帖子
)})}
导出默认主页;

相关问题