我最近决定用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;
2条答案
按热度按时间kx1ctssn1#
尝试删除
.todos
,以便变成:
czfnxgou2#
从“react”导入React从“easy-peasy”导入{ useStoreState };
从'./Feed'导入源const主页=({正在加载,获取错误})=〉{ const {搜索结果} = useStoreState((状态)=〉状态.搜索结果)return({正在加载帖子...
}; {获取错误&&〈p类别名称=“状态消息”样式={{颜色:“红色”}}〉{获取错误}
}; {!正在加载&&!提取错误&&(搜索结果长度?:没有要显示的帖子
)})}
导出默认主页;