class FormWidget extends Component {
state={
isFormVisible: false,
};
handleButtonClick = () => {
this.setState(prevState => ({
isFormVisible: !prevState.isFormVisible,
}));
}
render() {
const { isFormVisible } = this.state;
const buttonText = isFormVisible ? 'Hide form' : 'Show form';
return (
<Wrapper>
<Button onClick={this.handleButtonClick}>
{buttonText}
</Button>
<Form isVisible={isFormVisible} />
</Wrapper>
)
}
}
import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import { someAcion } from '../somePlace';
class App extends Component {
...
componentWillReciveProps(nextProps) {
if (this.props.location !== nextProps.location) {
this.props.someAciton();
}
}
...
}
const mapDispatchToProps = {
someAction,
};
export default withRouter(connect(null, mapDispatchToProps)(App));
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);
const array = [{ name: 'John', age: 32}];
const element = { name: 'Sally', age: 25 };
const newArray = [ ...array, element ];
// => [{ name: 'John', age: 32}, { name: 'Sally', age: 25 }]
(state: S, action: A) => S
const isInputActiveReducer = (state = false, action) => {
const { payload, type } = action;
switch(type) {
case TOGGLE_INPUT:
return payload;
default:
return state;
}
}
componentDidMount() {
const ideas = firebase.database().ref("items");
let arr = [];
ideas.on("value", snapshot => {
let items = snapshot.val();
for (let item in items) {
arr.push({
id: item,
text: items[item].text
});
}
this.props.renderIdeas(arr);
});
}
Object.keys(obj).map(key => ({ id: key, text: obj[key].text }));
<Route path="/post/:postId" component={Post}/>
const Post = ({ match, postsMap }) => {
const post = postsMap[match.params.postId];
return (
<div>
<h1>{post.title}</h1>
<p>{post.body}</p>
</div>
}
<List>
{postsList.map(post => (
<Link to={`/post/${post.id}`} key={post.id}>{post.title}</Link>
))}
</List>
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.
this.setState((prevState) =>({
currentPage : prevState.currentPage + 1
}), this.updateOrders);
getActivePage = activePage => {
if (this.state.currentPage === activePage) return;
this.setState({
currentPage : activePage,
}, this.updateOrders);
};
fetchOrders = () => {
const {
currentPage,
ordersPerPage,
sortBy,
sortDirection,
} = this.state;
this.props.fetchOrders({
page: currentPage,
limit: ordersPerPage,
sortBy,
sortDirection,
});
};
class ColumnsPanel extends Component {
...
handleColumnButtonClick = e => {
...
};
...
render() {
...
return(
<ColumnButton
onClick={this.handleColumnButtonClick}
/>
...
)
}
}
import {ADD_USER} from '../constans/appConstants';
export const addUser = (name, age) => {
return {
type: ADD_USER,
payload: {
name: name,
age: age,
},
}
};