<List>
{data.map(item => <Item key={item.id} item={item} />)}
</List>
function defaultEqualityCheck(a, b) {
return a === b
}
class Example extends React.Component {
state = { filter: 'all' };
componentDidMount() {
this.props.fetchData(this.state.filter);
}
componentDidUpdate(prevProps, prevState) {
if (prevState.filter !== this.state.filter) {
this.props.fetchData(this.state.filter);
}
}
render() { /* ... */ }
}
prevProp === prop
class Example extends React.Component {
state = { data: [] };
controller = new AbortController();
componentDidMount() {
fetch('/url', {
signal: this.controller.signal,
})
.then(res => res.json())
.then(data => this.setState({ data }));
}
componentWillUnmount() {
this.controller.abort();
}
render(){
return ( /* ... */ );
}
}
Так вот, будут ли ПРОСТЫЕ примеры реализации токен авторизации между этими двумя фреймворками?
import React from 'react';
import Cookie from 'js-cookie';
class SimpleExample extends React.Component {
state = {
isProcessing: false,
token: Cookie.get('token'),
data: null,
};
handleSignIn = () => {
this.setState({ isProcessing: true });
fetch('/singin', {
method: 'POST',
data: JSON.stringify({ login: 'admin', pass: 'zx23ww' }),
})
.then(res => res.json())
.then(data => {
this.setState({
isProcessing: false,
token: data.token,
});
Cookie.set('token', token);
});
};
handleLogout = () => {
Cookie.erase('token');
this.setState({ token: null });
};
handleGetProtectedData = () => {
fetch('/protected'{
method: 'GET',
headers: {
Authorization: `Bearer ${this.state.token}`,
},
})
.then(res => res.json())
.then(data => {
this.setState({ data });
});
};
render() {
const { isProcessing, token } = this.state;
if (isProcessing) return <div>...initialization</div>;
const isSignedIn = !!token;
return isSignedIn ? (
<div>
<h1>You're signed in</h1>
<button onClick={this.handleLogout}>Logout</button>
<button onClick={this.handleGetProtectedData}>Get protected data</button>
</div>
) : (
<div>
<h1>You're not signed in</h1>
<button onClick={this.handleSignIn}>Sign in</button>
</div>
);
}
}
<Link to={`/tasks/${item.id}`}>Open</Link>
<Route exact path="tasks/:id" component={TaskDetails} />
import React from 'react';
import tasks from '../data/tasks';
const TaskDetails = ({ match }) => {
const { id } = match.params;
const task = tasks.find(item => item.id === id);
return ( /* ... */ );
}
export default TaskDetails;