freeCodeCamp/curriculum/challenges/chinese/03-front-end-libraries/react-and-redux/connect-redux-to-the-messag...

6.0 KiB
Raw Blame History

id title challengeType isRequired videoUrl localeTitle
5a24c314108439a4d4036148 Connect Redux to the Messages App 6 false 将Redux连接到消息应用程序

Description

现在您已了解如何使用connect将React连接到Redux您可以将您学到的知识应用于处理消息的React组件。在上一课中您连接到Redux的组件名为Presentational ,这不是任意的。该术语通常是指未直接连接到Redux的React组件。他们只负责UI的呈现并根据他们收到的道具来执行此操作。相比之下容器组件连接到Redux。这些通常负责将操作分派给商店并且经常将商店状态作为道具传递给子组件。

Instructions

到目前为止代码编辑器包含了您在本节中编写的所有代码。唯一的变化是React组件被重命名为Presentational 。创建一个名为Container的常量中保存的新组件,该常量使用connectPresentational组件连接到Redux。然后AppWrapper 渲染React Redux Provider组件。将Redux store Provider作为道具传递,并将Container作为子项呈现。设置完所有内容后,您将再次看到应用程序呈现给页面的消息。

Tests

tests:
  - text: <code>AppWrapper</code>应该呈现给页面。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("AppWrapper").length === 1; })(), "The <code>AppWrapper</code> should render to the page.");'
  - text: <code>Presentational</code>组件应呈现<code>h2</code>  <code>input</code>  <code>button</code>和<code>ul</code>元素。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); return mockedComponent.find("Presentational").length === 1; })(), "The <code>Presentational</code> component should render an <code>h2</code>, <code>input</code>, <code>button</code>, and <code>ul</code> elements.");'
  - text: <code>Presentational</code>组件应呈现<code>h2</code>  <code>input</code>  <code>button</code>和<code>ul</code>元素。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); return ( PresentationalComponent.find("div").length === 1 && PresentationalComponent.find("h2").length === 1 && PresentationalComponent.find("button").length === 1 && PresentationalComponent.find("ul").length === 1 ); })(), "The <code>Presentational</code> component should render an <code>h2</code>, <code>input</code>, <code>button</code>, and <code>ul</code> elements.");'
  - text: <code>Presentational</code>组件应该从Redux商店接收<code>messages</code>作为道具。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return Array.isArray(props.messages); })(), "The <code>Presentational</code> component should receive <code>messages</code> from the Redux store as a prop.");'
  - text: <code>Presentational</code>组件应该接收<code>submitMessage</code>操作创建者作为prop。
    testString: 'assert((function() { const mockedComponent = Enzyme.mount(React.createElement(AppWrapper)); const PresentationalComponent = mockedComponent.find("Presentational"); const props = PresentationalComponent.props(); return typeof props.submitNewMessage === "function"; })(), "The <code>Presentational</code> component should receive the <code>submitMessage</code> action creator as a prop.");'

Challenge Seed

// Redux:
const ADD = 'ADD';

const addMessage = (message) => {
  return {
    type: ADD,
    message: message
  }
};

const messageReducer = (state = [], action) => {
  switch (action.type) {
    case ADD:
      return [
        ...state,
        action.message
      ];
    default:
      return state;
  }
};

const store = Redux.createStore(messageReducer);

// React:
class Presentational extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: ",
      messages: []
    }
    this.handleChange = this.handleChange.bind(this);
    this.submitMessage = this.submitMessage.bind(this);
  }
  handleChange(event) {
    this.setState({
      input: event.target.value
    });
  }
  submitMessage() {
    const currentMessage = this.state.input;
    this.setState({
      input: ",
      messages: this.state.messages.concat(currentMessage)
    });
  }
  render() {
    return (
      <div>
        <h2>Type in a new Message:</h2>
        <input
          value={this.state.input}
          onChange={this.handleChange}/><br/>
        <button onClick={this.submitMessage}>Submit</button>
        <ul>
          {this.state.messages.map( (message, idx) => {
              return (
                 <li key={idx}>{message}</li>
              )
            })
          }
        </ul>
      </div>
    );
  }
};

// React-Redux:
const mapStateToProps = (state) => {
  return { messages: state }
};

const mapDispatchToProps = (dispatch) => {
  return {
    submitNewMessage: (newMessage) => {
       dispatch(addMessage(newMessage))
    }
  }
};

const Provider = ReactRedux.Provider;
const connect = ReactRedux.connect;

// define the Container component here:


class AppWrapper extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    // complete the return statement:
    return (null);
  }
};

After Test

console.info('after the test');

Solution

// solution required