Answer the question
In order to leave comments, you need to log in
How to deal with Redux?
Help me understand Redax?
I reviewed all the examples, translated almost the entire "Basics" section of the documentation, I seem to understand something, but I can't do anything.
Let's say we have a component directory (I used the bootstrap classes):
/* card.js */
import React, { Component } from 'react';
var pictures = [
{descr: 'Стильный и модный', title: 'Iphone 6', id: 0, src: 'http://content2.onliner.by/catalog/device/header/5c89a202887278d66e83e9ea119a537d.jpg'},
{descr: 'Стильный и модный 2', title: 'Iphone 5s', id: 1, src: 'http://content2.onliner.by/catalog/device/header/f16849c646ec7b06d7ceaa62c1a1b883.jpg'},
{descr: 'Стильный и модный 3', title: 'Iphone 5', id: 2, src: 'http://content2.onliner.by/catalog/device/header/98b010c3ae263120c548aa3856fefc2f.jpg'},
];
export default class Card extends Component {
render() {
return (
<div>
<div>
{pictures.map(picture => (
<div className="col-sm-4 col-md-4">
<div className="thumbnail">
<img src={picture.src} height="100" alt="..."/>
<div className="caption">
<h3>{picture.title}</h3>
<p>{picture.descr}</p>
<p><a href='#'
className="btn btn-primary" role="button">Подробнее</a></p>
</div>
</div>
</div>
))}
</div>
</div>
);
}
}
/* App.js */
import React, { Component } from 'react';
import Card from './card';
export default class App extends Component {
render() {
return (
<Card/>
);
}
}
/*index.js*/
import React from 'react';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import todoApp from './reducers';
import App from './App';
let store = createStore(todoApp);
let rootElement = document.getElementById('root');
React.render(
// The child must be wrapped in a function
// to work around an issue in React 0.13.
<Provider store={store}>
{() => <App />}
</Provider>,
rootElement
);
/*actions.js*/
export const SOME_ACTION = 'SOME_ACTION';
/*
* action creators
*/
export function completeTodo(index) {
return { type: SOME_ACTION, index };
}
/*reducers.js*/
import { combineReducers } from 'redux';
import { SOME_ACTION} from './actions';
function todos(state = [], action) {
switch (action.type) {
{/*этот редьюсер написал не я*/}
case SOME_ACTION:
return [
...state.slice(0, action.index),
Object.assign({}, state[action.index], {
completed: true
}),
...state.slice(action.index + 1)
];
default:
return state;
}
}
const todoApp = combineReducers({
todos
});
export default todoApp;
Answer the question
In order to leave comments, you need to log in
Good afternoon, I'll do my bit - https://www.gitbook.com/book/maxfarseer/redux-cour...
A detailed tutorial (in Russian) on how to make an application using VK API on
redux everything you need to comfortably write applications without Google. If there are questions, I will be happy to answer.
In this reducer, you take an object with index index from the state array and set completed = true for it. This is the processing of the action by the reducer.
This is how, for example, the reducer that saves the user to the state will look like:
function todos(state = [], action) {
switch (action.type) {
/*этот редьюсер написал я*/
case ADD_USER_ACTION:
return [ ...state, action.user ];
default:
return state;
}
}
@connect( state => ({ pictures: state.myPicturesReducerName }) )
export default class Card extends Component {
// ...
// yourPicturesReducer state -> this.props.pictures
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question