React Redux:获取道具和更新状态 [英] React Redux: Getting Props And Updating State
问题描述
我第一次尝试使用React / Redux / JS。我对组件中的状态设置与让redux更新它有点困惑。
I am trying out React/Redux/JS for the first time. I am a little confused about setting state in component vs letting redux update it.
我想点击一个按钮将lightOn设置为true并更新this.props .lightOn值显示。我遗漏了一些根本但不确定的东西。
I want to click a button to set the lightOn to true and have the updated this.props.lightOn value display. I am missing something fundamental but not sure what.
操作:
export const setLight = lightStatus => dispatch => {
const res = lightStatus;
console.log(res); // => true on click
dispatch({ type: SET_LIGHT, payload: res });
};
在组件中,{this.props.onLight}未定义,所以我没有正确更新状态:
In the component, {this.props.onLight} is undefined so I am not updating state correctly:
import React, { Component } from 'react';
import { connect } from 'react-redux';
//import * as actions from '../actions';
import { setLight } from '../actions';
import { bindActionCreators } from 'redux';
class Light extends Component {
render() {
return (
<div>
Light Status: {this.props.lightOn}
<button
className="btn"
onClick={() => this.props.setLight(true)}
>
Set Light!
</button>
</div>
);
}
}
function mapStateToProps(state) {
return {
lightOn: state.lightOn
};
}
function mapDispatchToProps(dispatch) {
//whenever selectBook is called the result should be passed to all of our reducers
return bindActionCreators({ setLight: setLight }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Light);
reducer(使用点差运算符更新):
the reducer (updated with spread operator):
import { SET_LIGHT } from '../actions/types';
export default function(state = [], action) {
switch (action.type) {
case SET_LIGHT:
return { ...state, lightOn: action.payload };
default:
return state;
}
}
推荐答案
用数组设置状态我现在用对象设置。我想我将lightOn对象放在光阵列中,这使得它无法访问。
Instead of setting state with array I am now setting with object. I guess I was putting lightOn object in light array which was making it inaccessible.
操作:
export const setLight = lightStatus => dispatch => {
dispatch({ type: SET_LIGHT, payload: lightStatus });
};
reducer:
import { SET_LIGHT } from '../actions/types';
export default function(state = { on: false }, action) {
console.log('state ', state);
switch (action.type) {
case SET_LIGHT:
return { ...state, on: action.payload };
default:
return state;
}
}
组件:
import React, { Component } from 'react';
import { connect } from 'react-redux';
//import * as actions from '../actions';
import { setLight } from '../actions';
import { bindActionCreators } from 'redux';
class Light extends Component {
render() {
return (
<div>
Light On: {this.props.light.on.toString()}
<button
className="btn"
onClick={() => this.props.setLight(!this.props.light.on)}
>
Set Light!
</button>
</div>
);
}
}
function mapStateToProps(state) {
return { light: state.light };
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ setLight: setLight }, dispatch);
}
export default connect(mapStateToProps, mapDispatchToProps)(Light);
这篇关于React Redux:获取道具和更新状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!