class Child extends Component {
render() {
const { handleChange } = this.props;
return (
<Wrapper>
<SomeInput onChange={handleChange} />
</Wrapper>
);
}
}
handleChange = e => {
const { name, value } = this.props;
this.setState({ [name]: value }, this.calculate);
};
/common
/api
/components
/ducks
/entities
/sagas
/selectors
/utils
/features
/Feature1
/Feature2
/Feature3
/Feature4
...
/FeatureN
/Main
/pages
index.js
App.js
routes.js
rootReducer.js
rootSaga.js
store.js
/Auth
/pages
index.js
App.js
routes.js
rootReducer.js
rootSaga.js
store.js
...
/features
/Accounts
/components
index.js
accountsDucks.js
accountsSaga.js
accountsSelectors.js
accountsApi.js
Accounts.js
AccountsContainer.js
/actions
/common
/components
/core
/Feed
/Profile
...
/constraints
/containers
/entries
/locales
/pages
/reducers
/utils
...
app.use((req, res, next) => {
console.log('Time: ', Date.now());
next();
});
app.use(express.static(__dirname + '/public'));
app.get("/about", (req, res) => { /* */}); // ( 1 )
app.get("/home", (req, res) => { /* */}); // ( 2 )
app.get("*", (req, res) => { /* */}); // ( 3 )
app.get("/never", (req, res) => { /* */}); // ( 4 )
GET https://site/public/img/1.png
GET https://site/home
GET https://site/feed
GET https://site/never
Time: <текущее время>
export const withLatest = request => {
let last = 0;
return (...args) =>
new Promise((resolve, reject) => {
const current = ++last;
request(...args)
.then(res => resolve({ isLatest: current === last, res }))
.catch(err => reject({ isLatest: current === last, err }));
});
};
export const withLatest = request => {
let last = 0;
return async (...params) => {
const current = ++last;
try {
const res = await request(...params);
return { isLatest: current === last, res };
} catch (err) {
throw { isLatest: current === last, err };
}
};
};
import Api from '../api';
import { withLatest } from '../utils';
const withLatestFetchData = withLatest(Api.fetchData);
export const fetchData = query => async (dispatch, state) => {
dispatch({ type: "LOADING_DATA" });
try {
const { isLatest, res } = await withLatestFetchData(query);
if (isLatest) {
dispatch({ type: "DATA_SUCCESS", payload: res });
}
} catch ({ isLatest, err }) {
if (isLatest) {
console.log(err);
}
}
};
немного подумав решил заменить конструкцию
Array(1000)].map(_ => <SomeComponent />);
obj.method(); // вызываем метод
this.method(); // вызываем метод
onClick={obj.method} // не вызываем метод
onClick={this.method} // не вызываем метод
class MainPage extends Component {
constructor(props) {
super(props);
this.state = {
color: 'red'
};
this.changeStylesForCaption = this.changeStylesForCaption.bind(this); // привязываем контекст
}
changeStylesForCaption() {
this.setState({
color: 'black'
});
}
render() {
return (
<h1 onClick={this.changeStylesForCaption} style={{ color: this.state.color }}>gleb</h1>
);
}
}
const Example extends Component {
state = {
select: 'default value',
};
postData(data) {
fetch('api/somePath', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}).then(res => res.json())
.then(data => {
// do something with data
});
}
handleChange = e => { /* ... */ };
handleSubmit = () => {
const { select } = this.state;
this.postData({ select });
};
render() {
return (
<Form>
<Select value={this.state.select} onChange={this.handleChange} />
<Button onClick={this.handleSubmit}>Submit</Button>
</Form>
);
}
}
export const store = configureStore();
// some code
import { store } from './index';
import { someAction } from './actions/some';
store.dispatch(someAction());
Как работают Thunk?
Правильно ли я понимаю, что этот код будет выполнен синхронно