class ActionsReducers extends React.Component {
handleAddMan = () => {
this.props.addMan({ name: 'Альберт', family: 'Эйнштейн' });
};
render() {
const { men } = this.props;
return (
<Wrapper>
<List>
{men.map(man => <Man key={man.id} man={man} />)}
{!men.length && <Empty>No data in list.</Empty>}
</List>
<Button onClick={this.handleAddMan}>Add man</Button>
</Wrapper>
);
}
}
class ActionsReducers extends React.Component {
componentDidMount() {
this.props.addMan({ name: 'Альберт', family: 'Эйнштейн' });
}
render() {
const { men } = this.props;
return (
<Wrapper>
<List>
{men.map(man => <Man key={man.id} man={man} />)}
{!men.length && <Empty>No data in list.</Empty>}
</List>
</Wrapper>
);
}
}
const mapStateToProps = state => ({
userData: state.userData,
});
import { createSelector } from 'reselect';
export const userDataSeleror = state => state.userData;
export const userSelector = state => createSelector(
userDataSelector,
userData => userData.user,
);
export const isUserDataLoadingSelector = state => createSelector(
userDataSelector,
userData => userData.isLoading,
);
export const isUserDataErrorSelector = state => createSelector(
userDataSelector,
userData => userData.isError,
);
import { fetchUserData } from './somePlace';
import Profile from './Profile';
import {
userSelector,
isUserDataLoadingSelector,
isUserDataErrorSelector,
} from './selectors';
const mapStateToProps = state => ({
user: userSelector(state),
isLoading: isUserDataLoadingSelector(state),
isError: isUserDataErrorSelector(state),
});
const mapDispatchToProps = {
fetchUserData,
};
export default connect(mapStateToProps, mapDispatchToProps)(Profile);
class Profile extends Component {
componentDidMount() {
this.props.fetchUserData();
}
render() {
const { user, isLoading, isError } = this.props;
return (
...
);
}
}
function Block({
children,
isLoading,
isError,
isEmpty,
emptyText,
fetch,
}) {
const showPreloader = isLoading && !isError;
const allDataIsReady = !isLoading && !isError;
const showEmptyText = allDataIsReady && isEmpty && emptyText;
return (
<Wrapper>
{showPreloader && <Preloader />}
{isError && <TryAgain fetch={fetch} />}
{showEmptyText && <Empty>{emptyText}</Empty>}
{allDataIsReady && children}
</Wrapper>
);
}
render() {
const { isLoading, isError, orderList } = this.props;
return (
<Block
isLoading={isLoading}
isError={isError}
isEmpty={!ordersList.length}
fetch={this.fetchOrdersList}
emptyText="You don't have any orders."
>
{ordersList.map(order => <Order key={order.id} order={order} />}
</Block>
)
}
export const routes = [
{
component: Home,
path: '/',
},
{
component: Roster,
path: '/roster',
},
{
component: Schedule,
path: '/schedule',
},
];
export default function SwitchWithRoutes({ routes }) {
return (
<Switch>
{routes.map((route, i) => (
<Route
key={i}
exact={route.path === '/'}
path={route.path}
component={route.component}
/>
)}
</Switch>
);
}
it('test openModal', () => {
expect(actions.openModal({ name: 'Some name'}, '1')).toEqual({
type: types.OPEN_MODAL,
payload: {
id: '1',
initData: {
name: 'Some name',
},
},
});
});
{
type: types.OPEN_MODAL, // тут естественно значение
payload: {
id: undefined,
initData: {
id: '1',
initData: {
name: 'Some name',
},
},
},
}
export default function(state = initialState, action) {
if (action.type === 'DIALOG_SELECTED') {
return state;
}
if (action.type === 'DIALOG_DELETE') {
const newState = [...state];
newState.shift();
return newState;
}
else {
return state;
}
}
const mapStateToProps = state => ({
dialogs: state.dialogs,
});
const mapDispatchToProps = {
selectDialog: actions.selectDialog,
deleteDialog: actions.deleteDialog,
};
export default connect(mapStateToProps, mapDispatchToProps)(Sidebar);
class Sidebar extends Component {
selectDialog = () => {
this.props.selectDialog();
};
deleteDialog = () => {
this.props.deleteDialog();
};
render() {
console.log('рендер сайдбара');
return (
<div className='sidebar'>
<div className='sidebar__content'>
<div className='sidebar__head'></div>
<SidebarChatGroup chatName='Общий'>
<button onClick={this.selectDialog}>Выбрать</button>
<button onClick={this.deleteDialog}>удалить</button>
</SidebarChatGroup>
</div>
<div className='sidebar__bg'></div>
</div>
)
}
}
const FETCH_USER_DATA_REQUEST = 'FETCH_USER_DATA_REQUEST';
const FETCH_USER_DATA_SUCCESS = 'FETCH_USER_DATA_SUCCESS';
const FETCH_USER_DATA_FAIL = 'FETCH_USER_DATA_FAIL';
export const fetchUserData = () => ({
type: FETCH_USER_DATA_REQUEST,
});
export const fetchUserDataSuccess = data => ({
type: FETCH_USER_DATA_SUCCESS,
payload: data,
});
export const fetchUserDataFail = () => ({
type: FETCH_USER_DATA_FAIL,
});
const initialState = {
data: {},
isLoading: true,
isError: false,
};
export default (state = initialState, action) => {
const { type, payload } = action;
switch (action) {
case FETCH_USER_DATA_REQUEST:
return {
...state,
isLoading: true,
};
case FETCH_USER_DATA_SUCCESS:
return {
...payload,
isLoading: false,
isError: false,
};
case FETCH_USER_DATA_FAIL:
return {
...state,
isLoading: false,
isError: true,
};
default:
return state;
}
};
import { call, put, takeLatest } from 'redux-saga/effects';
import {
FETCH_USER_DATA_REQUEST,
fetchUserDataSuccess,
fetchUserDataFail,
} from '../ducks/userDucks';
import { fetchUserData } from '../api';
export function* fetchUserDataSaga() {
try {
const { data } = yield call(fetchUserData);
yield put(fetchUserDataSuccess(data));
} catch (error) {
yield put(fetchUserDataFail());
}
}
export default function* watchFetchUserDataSaga() {
yield takeLatest(FETCH_USER_DATA, fetchUserDataSaga);
}
export const userDataSelector = state => state.userData;
import User from './User';
import { userDataSelector } from '../selectors';
const mapStateToProps = state => ({
userData: userDataSelector(state),
});
export default connect(mapStateToProps)(User);
import Settings from './Settings';
import { userDataSelector } from '../selectors';
import { fetchUser } from '../ducks/userDucks';
const mapStateToProps = state => ({
userData: userDataSelector(state),
});
const mapDispatchToProps = {
fetchUserData,
};
export default connect(mapStateToProps, mapDispatchToProps)(Settings);
(state: S, action: A) => S
const isInputActiveReducer = (state = false, action) => {
const { payload, type } = action;
switch(type) {
case TOGGLE_INPUT:
return payload;
default:
return state;
}
}
import { connect } from 'react-redux';
import { inputIdeaHandle } from '../actions';
const mapDispatchToProps = {
inputIdeaHandle,
};
export default connect(null, mapDispatchToProps)(Dashboard);
changeHandle = e => {
const { value } = e.target;
this.props.inputIdeaHandle(value);
};
Use babel-plugin-syntax-dynamic-import on the client.
Use babel-plugin-dynamic-import-node on the server.
import {ADD_USER} from '../constans/appConstants';
export const addUser = (name, age) => {
return {
type: ADD_USER,
payload: {
name: name,
age: age,
},
}
};