我想将本地状态值分配给redux状态值,当我编写console.log(action)以了解天气动作是否获取我的值时,它会显示它获取我的值但是redux状态不分配那些价值!请给出解决方案......
App.js:
import React,{Component} from "react";
import {connect} from "react-redux";
import {bindActionCreators} from "redux";
import {addName} from './action';
class App extends Component {
constructor(props){
super(props);
this.state = {
first:''
};
}
inputData = event => {
this.setState({
first:event.target.value
});
}
submitData = event => {
event.preventDefault();
this.props.addName({
firstName:this.state.first
});
console.log(this.props.firstName)
}
render(){
return(
<div>
<form onSubmit={this.submitData}>
<input type="text" name="first" onChange={this.inputData}/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
function mapStateToProps(state){
return {
firstName : state.firstName
};
};
function mapDispatchToProps(dispatch) {
return{
addName: bindActionCreators(addName,dispatch)
};
};
export default connect(mapStateToProps,mapDispatchToProps)(App)
主要index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {Provider} from "react-redux";
import {rootReducer} from './reducers/name';
import {createStore} from "redux";
import registerServiceWorker from './registerServiceWorker';
let store = createStore(rootReducer)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root'));
registerServiceWorker();
reducer name.js:
const init = {firstName: ''};
export let rootReducer = (state=init,action) => {
switch(action.type)
{
case 'ADD_ENTRY':
{
return{
firstName:action.firstName
};
}
default:
{
return state;
}
}
}
我的动作组件index.js
export const addName = action => {
return{
type:'ADD_ENTRY',
...action
};
};
请检查当我尝试使用action.firstName进行分配时,我认为分配没有完成,所以请正确检查我的reducer。
答案 0 :(得分:1)
这是因为你没有更新reducers状态,你只是从动作返回传递给reducer的值。相反,您需要先更新(timeout 20 docker stats --format \
'CPU: {{.CPUPerc}}\tMEM: {{.MemPerc}}' <CONTAINER_ID> \
| sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' ; echo) \
| gzip -c > monitor.log.gz
,然后返回整个减速器状态:
修改强>:
state.firstName