问题描述
我在 mapStatetoProps 中遇到问题,它在控制台中显示未定义。我已将 console.log 放在 app.js 中,显示 loginval 未定义。它只是一个简单的 react redux 代码。我找不到我的错误。 我已经尝试了我所知道的一切,但它仍然未定义。
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import store from './redux/store';
import {Provider} from 'react-redux';
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,document.getElementById('root')
);
// If you want to start measuring performance in your app,pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint.
reportWebVitals();
app.js
import './App.css';
import React,{Component} from 'react';
import {Route,withRouter} from 'react-router-dom';
import {Provider,connect} from 'react-redux';
class App extends Component{
render(){
console.log(this.props.loginval);
return (
<div className="App">
HELLO
</div>
);
}
}
const mapStatetoProps = state => {
return {
loginval:state.loginval
};
};
export default connect(mapStatetoProps)(App);
store.js
import {createStore} from 'redux';
import changeLogin from './reducer/loginValue';
const store=createStore(changeLogin,window.__Redux_DEVTOOLS_EXTENSION__ && window.__Redux_DEVTOOLS_EXTENSION__());
export default store;
登录值.js
const initialState={
loginval:true
};
const changeLogin = (state=initialState,action) => {
switch(action.type){
case "Change": return !state.loginval;
default: return state.loginval;
}
};
export default changeLogin;
行动
export const changelogin=()=>{
return{
type:"Change"
}
}
解决方法
这里的reducer需要返回新的状态,但它只返回一个布尔值。我认为你需要类似的东西
fig.update_traces(text = df['Gender'].value_counts(),textinfo = 'label+percent+value')