私は初めてReact/Redux/JSを試しています。私はコンポーネントの状態を設定することについて少し混乱しています。React Redux:小道具の入手と状態の更新
ボタンをクリックしてlightOnをtrueに設定し、更新されたthis.props.lightOnの値を表示したいとします。私は基本的な何かを欠いているが、何が分からないのか。
アクション:
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);
を(拡散演算子を使用して更新)減速:成分で
export const setLight = lightStatus => dispatch => {
const res = lightStatus;
console.log(res); // => true on click
dispatch({ type: SET_LIGHT, payload: res });
};
、{this.props.onLightは}私は正常状態を更新するわけではない未定義です。
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;
}
}
何?ヘックスはレフィックスを使用しています。 Hesはこれを処理する内部状態の方法を探していません... – WilomGfx