我无法在 React 组件中正确访问 redux 存储
I can't access the redux store properly in react components
我刚开始在我的 React 应用程序中使用 redux,我已经成功地在我的 redux 上添加了一些值 store.On 发生调度的同一组件我可以通过
访问商店
store.getState();
但在其他组件上我无法通过 mapStateToProps 或上述方法访问它。我真的很想知道为什么会这样。
index.js
const rootElement = document.getElementById("root");
ReactDOM.render(
<Provider store={store} > <App /> </Provider>, rootElement);
store.js
import { createStore } from "redux";
import rootReducer from "../reducers/index";
const store = createStore(rootReducer);
export default store;
reducer.js
const initialState = {
token:"",email:"",uid:""
};
function userReducer(state = initialState, action) {
console.log("check ", state, action);
switch(action.type) {
case "ADD_USER":
return Object.assign({}, state, {
token : action.token,
email : action.email,
uid : action.uid
});
default : return state;
}
}
export default userReducer;
action.js
const addUser = (token,email,uid) => ({
type:"ADD_USER",token:token,email : email,uid:uid
})
export default addUser;
login.js
function mapDispatchToProps (dispatch) {
console.log(dispatch);
return { addUser : (token,email,uid)=> dispatch(addUser(token,email,uid))
};}
class Sample extends React.Component {
constructor(props){
super(props);
this.state = {...........}
}
componentDidMount() {
let token = localStorage.getItem("myToken");
let user = decode(token);
let uid = user.id;
let email = user.email;
this.props.addUser(token,email,uid);
console.log(this.props.state);
console.log(store.getState());
}
}
const mapStateToProps = state => {
return {state:state}
}
export default connect(mapStateToProps,mapDispatchToProps)(Sample);
anotherPage.js
export default function AnPage() {
const Data = useSelector(state=>state.userReducer);
useEffect(()=> {
somFunct(); },[]);
}
someFunct=() => {
console.log(Data) =>output is ({token: "", email: "", uid: ""})
return(
)
}
reducer.js
处的控制台输出
check {token: "", email: "", uid: ""}token: ""email: ""uid: ""__proto__: Object {type: "ADD_USER",
token: "*******", email: "dfgsdhf@gmail.com", uid: 6264}
console.log(this.props.state)->
userReducer: {token: "", email: "", uid: ""}
__proto__: Object
console.log(store.getState()) ->
userReducer: {token: "*******", email: "dfgsdhf@gmail.com", uid: 6234}
__proto__: Object
我已经编辑了问题。
您不应该在构造函数中再次声明状态。您将使用 mapStateToProps 方法从道具中获取状态。
export const mapStateToProps = function(state) {
return {
token: state.token,
email: state.email,
uid: state.uid
};
};
class Sample extends React.Component {
constructor(props){
super(props);
}
我发现 state 的初始值作为其他组件输出的原因是因为每次有新组件时我都在刷新页面 loaded.Since redux states 有一个特殊的刷新 时擦除状态的行为 我必须从 react-router-dom 添加 'Link' 以避免刷新,如果由于其他原因刷新,则使用 redux-persist 库加载状态。
我希望这对遇到此类问题的人有所帮助。
我刚开始在我的 React 应用程序中使用 redux,我已经成功地在我的 redux 上添加了一些值 store.On 发生调度的同一组件我可以通过
访问商店 store.getState();
但在其他组件上我无法通过 mapStateToProps 或上述方法访问它。我真的很想知道为什么会这样。
index.js
const rootElement = document.getElementById("root");
ReactDOM.render(
<Provider store={store} > <App /> </Provider>, rootElement);
store.js
import { createStore } from "redux";
import rootReducer from "../reducers/index";
const store = createStore(rootReducer);
export default store;
reducer.js
const initialState = {
token:"",email:"",uid:""
};
function userReducer(state = initialState, action) {
console.log("check ", state, action);
switch(action.type) {
case "ADD_USER":
return Object.assign({}, state, {
token : action.token,
email : action.email,
uid : action.uid
});
default : return state;
}
}
export default userReducer;
action.js
const addUser = (token,email,uid) => ({
type:"ADD_USER",token:token,email : email,uid:uid
})
export default addUser;
login.js
function mapDispatchToProps (dispatch) {
console.log(dispatch);
return { addUser : (token,email,uid)=> dispatch(addUser(token,email,uid))
};}
class Sample extends React.Component {
constructor(props){
super(props);
this.state = {...........}
}
componentDidMount() {
let token = localStorage.getItem("myToken");
let user = decode(token);
let uid = user.id;
let email = user.email;
this.props.addUser(token,email,uid);
console.log(this.props.state);
console.log(store.getState());
}
}
const mapStateToProps = state => {
return {state:state}
}
export default connect(mapStateToProps,mapDispatchToProps)(Sample);
anotherPage.js
export default function AnPage() {
const Data = useSelector(state=>state.userReducer);
useEffect(()=> {
somFunct(); },[]);
}
someFunct=() => {
console.log(Data) =>output is ({token: "", email: "", uid: ""})
return(
)
}
reducer.js
处的控制台输出check {token: "", email: "", uid: ""}token: ""email: ""uid: ""__proto__: Object {type: "ADD_USER",
token: "*******", email: "dfgsdhf@gmail.com", uid: 6264}
console.log(this.props.state)->
userReducer: {token: "", email: "", uid: ""}
__proto__: Object
console.log(store.getState()) ->
userReducer: {token: "*******", email: "dfgsdhf@gmail.com", uid: 6234}
__proto__: Object
我已经编辑了问题。
您不应该在构造函数中再次声明状态。您将使用 mapStateToProps 方法从道具中获取状态。
export const mapStateToProps = function(state) {
return {
token: state.token,
email: state.email,
uid: state.uid
};
};
class Sample extends React.Component {
constructor(props){
super(props);
}
我发现 state 的初始值作为其他组件输出的原因是因为每次有新组件时我都在刷新页面 loaded.Since redux states 有一个特殊的刷新
我希望这对遇到此类问题的人有所帮助。