301 lines
8.3 KiB
Markdown
301 lines
8.3 KiB
Markdown
---
|
||
id: 5a24c314108439a4d4036148
|
||
title: Підключення Redux до додатку повідомлень
|
||
challengeType: 6
|
||
forumTopicId: 301427
|
||
dashedName: connect-redux-to-the-messages-app
|
||
---
|
||
|
||
# --description--
|
||
|
||
Тепер, коли ви розумієте, як використовувати `connect` щоб підключити React до Redux, ви можете застосувати набуті знання до вашого компоненту React, який обробляє повідомлення.
|
||
|
||
На попередньому уроці, компонент, який ви підключили до Redux мав назву `Presentational` і це не було довільним. Цей термін *зазвичай* посилається на компоненти React, які не підключені безпосередньо до Redux. Вони просто відповідають за представлення інтерфейсу користувача і виконують це в залежності від функції пропсів, які вони отримують. На відміну від цього, компонент container пов'язаний із Redux. Вони, зазвичай є відповідальними за відправлення дій у сховище даних і часто передають store state до дочірніх компонентів у якості пропсів.
|
||
|
||
# --instructions--
|
||
|
||
Редактор коду вже містить весь написаний вами код у цій секції. Єдина зміна полягає у тому, що компонент React перейменовується на `Presentational`. Створіть новий компонент, який знаходиться у константі під назвою `Container`, який використовує `connect`, щоб під'єднати компонент `Presentational` до Redux. Потім, у `AppWrapper`, відобразіть React Redux компонент `Provider`. Передайте `Provider` Redux `store` у якості пропсу і відобразіть `Container` як дочірній компонент. Коли все буде налаштоване, ви побачите додаток повідомлень виведеним на сторінку знову.
|
||
|
||
# --hints--
|
||
|
||
`AppWrapper` повинен відобразитися на сторінці.
|
||
|
||
```js
|
||
assert(
|
||
(function () {
|
||
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
||
return mockedComponent.find('AppWrapper').length === 1;
|
||
})()
|
||
);
|
||
```
|
||
|
||
Компонент `Presentational` повинен відобразитися на сторінці.
|
||
|
||
```js
|
||
assert(
|
||
(function () {
|
||
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
||
return mockedComponent.find('Presentational').length === 1;
|
||
})()
|
||
);
|
||
```
|
||
|
||
Компонент `Presentational` повинен відобразити елементи `h2`, `input`, `button` і `ul`.
|
||
|
||
```js
|
||
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
|
||
);
|
||
})()
|
||
);
|
||
```
|
||
|
||
Компонент `Presentational` повинен отримувати `messages` зі сховища Redux у якості пропсу.
|
||
|
||
```js
|
||
assert(
|
||
(function () {
|
||
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
||
const PresentationalComponent = mockedComponent.find('Presentational');
|
||
const props = PresentationalComponent.props();
|
||
return Array.isArray(props.messages);
|
||
})()
|
||
);
|
||
```
|
||
|
||
Компонент `Presentational` повинен отримувати `submitMessage` виконавця як пропс.
|
||
|
||
```js
|
||
assert(
|
||
(function () {
|
||
const mockedComponent = Enzyme.mount(React.createElement(AppWrapper));
|
||
const PresentationalComponent = mockedComponent.find('Presentational');
|
||
const props = PresentationalComponent.props();
|
||
return typeof props.submitNewMessage === 'function';
|
||
})()
|
||
);
|
||
```
|
||
|
||
# --seed--
|
||
|
||
## --after-user-code--
|
||
|
||
```jsx
|
||
ReactDOM.render(<AppWrapper />, document.getElementById('root'))
|
||
```
|
||
|
||
## --seed-contents--
|
||
|
||
```jsx
|
||
// 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() {
|
||
this.setState((state) => {
|
||
const currentMessage = state.input;
|
||
return {
|
||
input: '',
|
||
messages: 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);
|
||
}
|
||
};
|
||
```
|
||
|
||
# --solutions--
|
||
|
||
```jsx
|
||
// 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() {
|
||
this.setState((state) => {
|
||
const currentMessage = state.input;
|
||
return {
|
||
input: '',
|
||
messages: 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;
|
||
|
||
const Container = connect(mapStateToProps, mapDispatchToProps)(Presentational);
|
||
|
||
class AppWrapper extends React.Component {
|
||
constructor(props) {
|
||
super(props);
|
||
}
|
||
render() {
|
||
return (
|
||
<Provider store={store}>
|
||
<Container/>
|
||
</Provider>
|
||
);
|
||
}
|
||
};
|
||
```
|