简体   繁体   English

React-redux学习

[英]React-redux learning

) )

I'm currently learning React and Redux.我目前正在学习 React 和 Redux。 I'm writing my 1st app which is an elevator simulator.我正在编写我的第一个应用程序,它是一个电梯模拟器。 The goal is easy.目标很简单。 A keyboard allows the user to select the floor.一个键盘允许用户select地板。 Once selected a message is shown on a dashboard.选择后,仪表板上会显示一条消息。 After a delay the elevator should move to the floor requested (not implemented yet).延迟后,电梯应该移动到请求的楼层(尚未实施)。 A message is shown to monitor the current position of the elevator during the move (not implemented yet).显示一条消息以在移动期间监控电梯的当前 position(尚未实施)。
Currently the problem is when I click on a button, the state is not updated and I don't understand why:-(. If someone can point me where I'm wrong, thanks a lot !目前的问题是当我点击一个按钮时,state 没有更新,我不明白为什么:-(。如果有人能指出我错在哪里,非常感谢!

Dashboard code仪表板代码

import React from 'react';
import { connect } from "react-redux";
import { Container, Row, Col } from 'react-bootstrap';


const mapStateToProps = state => {
    return {
        elevator: state.elevator
        , inMove: state.inMove
    };
}

const ConnectedDashboard = ({ elevator, inMove }) => {
    return (
        <Container>
            <Row>
                <Col>Current position: {elevator.position}, floor selected: {elevator.floor_selected}, in move: {inMove.toString()} </Col>
            </Row>
        </Container>
        )
};

const Dashboard = connect(mapStateToProps)(ConnectedDashboard);

export default Dashboard;

Keyboard code键盘代码

import React, { Component } from 'react';
import { connect } from "react-redux";
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';

import { floorSelected } from '../Action/action';




function mapDispatchToProps(dispatch) {
    return {
        floorSelected: floor => dispatch(floorSelected(floor))
    };
}

const mapStateToProps = state => {
    return {
        elevator: state.elevator
        , inMove: state.inMove
    };
}


class ConnectedKeyboard extends Component {
    constructor(props) {
        super(props);
        this.state = {
            elevator: { position: 0, floor_selected: 0 }
            , inMove: false
        };

        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleSubmit(event) {
        event.preventDefault();
        this.props.floorSelected(event.target.value);
        this.setState({ elevator: { position: 0, floor_selected: event.target.value }, inMove: false })
    }

    render() {
        return (
            <Form>
                <Button variant="primary" type="submit" value="0" onSubmit={this.handleSubmit} >
                    0
      </Button>
                <Button variant="primary" type="submit" value="1" onSubmit={this.handleSubmit} >
                    1
      </Button>
                <Button variant="primary" type="submit" value="2" onSubmit={this.handleSubmit} >
                    2
      </Button>
                <Button variant="primary" type="submit" value="3" onSubmit={this.handleSubmit} >
                    3
      </Button>
                <Button variant="primary" type="submit" value="4" onSubmit={this.handleSubmit} >
                    4
      </Button>
                <Button variant="primary" type="submit" value="5" onSubmit={this.handleSubmit} >
                    5
      </Button>
                <Button variant="primary" type="submit" value="6" onSubmit={this.handleSubmit} >
                    6
      </Button>
                <Button variant="primary" type="submit" value="7" onSubmit={this.handleSubmit} >
                    7
      </Button>
                <Button variant="primary" type="submit" value="8" onSubmit={this.handleSubmit} >
                    8
      </Button>
                <Button variant="primary" type="submit" value="9" onSubmit={this.handleSubmit} >
                    9
      </Button>
            </Form>
        );
    }
}

const Keyboard = connect(mapStateToProps, mapStateToProps)(ConnectedKeyboard);

export default Keyboard;

The others elements of the app.应用程序的其他元素。 In the app, those elements are in separated files在应用程序中,这些元素位于单独的文件中

// App
import React from 'react';
import Keyboard from './Components/Keyboard';
import Dashboard from './Components/Dashboard';
import { Provider } from 'react-redux';
import store from './store';


import { createStore, combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';

function App() {
    return (
        <Provider store={store}>
            <Dashboard />
            <Keyboard />
        </Provider>
    );
}

export default App;


// Store
import { createStore } from "redux";
import rootReducer from "./Reducer/rootReducer";

const store = createStore(rootReducer);

export default store;

// Action
import { FLOOR_SELECTED } from '../Constants/action-type';

export function floorSelected(payload) {
    return { type: FLOOR_SELECTED, payload }
};

// Constants
export const FLOOR_SELECTED = "FLOOR_SELECTED";



// Reducers
import { FLOOR_SELECTED } from '../Constants/action-type';

const initialState = {
    elevator: { position: 0, floor_selected: 0 }
    , inMove: false
};


function rootReducer(state = initialState, action) {
    if (action.type === FLOOR_SELECTED) {
        return Object.assign({}, state, {
            elevator: action.payload
        });
    }
    return state;
};

export default rootReducer;

You are not adding your dispatch to your connect here:您没有在此处将您的调度添加到您的连接中:

const Keyboard = connect(mapStateToProps, mapStateToProps)(ConnectedKeyboard);常量键盘=连接(mapStateToProps,mapStateToProps)(ConnectedKeyboard);

you are using mapStateToProps twice, you need mapDispatchToProps in there as well.你使用mapStateToProps两次,你也需要mapDispatchToProps在那里。

function connect(mapStateToProps?, mapDispatchToProps?, mergeProps?, options?) as per react-redux docs function connect(mapStateToProps?, mapDispatchToProps?, mergeProps?, options?)根据react-redux 文档

After modifying the connect function as Doug suggested me, I modified handleSubmit function in the Keyboard.js在按照 Doug 的建议修改了连接 function 后,我在 Keyboard.js 中修改了句柄提交 function

 handleSubmit(event) { console.log("handleSubmit call..."); event.preventDefault(); this.props.floorSelected(event.nativeEvent.submitter.value); this.setState({ elevator: { position: 0, floor_selected: event.nativeEvent.submitter.value }, inMove: false }) }

And the reducer和减速机

 function rootReducer(state = initialState, action) { console.log("rootReducer call..."); console.log(action.type); if (action.type === FLOOR_SELECTED) { return Object.assign({}, state, { elevator: { position: state.elevator.position, floor_selected: action.payload } }); } return state; };

But what I still don't understand is in the handleSubmit is why to call the reducer then call setState?但是我仍然不明白的是在handleSubmit中为什么要调用reducer然后调用setState? I would say this line is not needed.我会说这条线是不需要的。 I commented out it and I get the same behavior.我评论了它,我得到了同样的行为。 What do you think?你怎么看?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM