[英]React-Redux: Can't use store
I'm new in React.我是 React 的新手。 And I'm studying Redux now.
我现在正在学习 Redux。 I understand how the library works, but I completely don't understand why my code doesn't.
我了解库的工作原理,但我完全不明白为什么我的代码不工作。 The problem is: React doesn't see the store .
问题是: React 没有看到 store 。 I can't use datas from the store.
我无法使用商店中的数据。
store.js:商店.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:索引.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:应用程序.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:主容器.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:减速器.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;
I tried to use connect() but had the same result.我尝试使用 connect() 但结果相同。
Thanks in advance for yours help.预先感谢您的帮助。
Thanks to all!谢谢大家! I mistaked in reducer.
我错在减速机。 I just realized that I did not return state
我才意识到我没有返回状态
store seems to be missing in the code that you provided.您提供的代码中似乎缺少商店。 Try this:
尝试这个:
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
Now to access your store, you can useSelector
or connect
from the react-redux
library现在访问你的店,你可以
useSelector
或connect
从react-redux
库
// Using useSelector // 使用 useSelector
import { useSelector } from 'react-redux'
const notes = useSelector(state => state.notes)
// Using connect // 使用连接
You first need to importe connect
from react-redux
like this:您首先需要像这样从
react-redux
导入connect
:
import {connect} from 'react-redux'
and use it like this并像这样使用它
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)
Remember to check out the the react-redux documentation记得查看react-redux 文档
Here's an example of how you could connect MainContainer
to your redux state.这是一个如何将
MainContainer
连接到您的 redux 状态的示例。
import { connect } from "react-redux";
const MainContainer = props => {
return (
<ul>
{props.notes.map(note => <li key={note.name}>{note.name}</li>)}
</ul>
);
}
const mapStateToProps = state => ({
notes: state.notes
})
export default connect(mapStateToProps)(MainContainer);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.