import React, { Component, PureComponent } from 'react';
import './App.css';
class Twitter extends PureComponent {
state = {
todos: null
}
async componentDidMount() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json());
this.setState({
todos: response
});
}
render() {
const {
children
} = this.props;
const {
todos
} = this.state;
return (
<React.Fragment>
{
React.cloneElement(children, { ...this.state })
}
</React.Fragment>
)
}
}
const UserProfile = (props) => {
const { todos } = props;
return (
<React.Fragment>
{
todos ? <h2>UserProfile</h2> : <div>Loading...</div>
}
</React.Fragment>
)
}
class App extends Component {
render() {
return (
<div className="App">
<Twitter username='user1'>
{/* {
(props) => {
return (
<h2>UserProfile</h2>
)
}
} */}
<UserProfile />
</Twitter>
</div>
);
}
}
export default App;
import React, { Component, PureComponent } from 'react';
import './App.css';
class Twitter extends PureComponent {
state = {
todos: null
}
async componentDidMount() {
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json());
this.setState({
todos: response
});
}
render() {
const {
children
} = this.props;
const {
todos
} = this.state;
return (
<React.Fragment>
{
React.cloneElement(children, { ...this.state })
}
</React.Fragment>
)
}
}
const UserProfile = (props) => {
const { todos } = props;
return (
<React.Fragment>
{
todos ? <h2>UserProfile</h2> : <div>Loading...</div>
}
</React.Fragment>
)
}
class App extends Component {
render() {
return (
<div className="App">
<Twitter username='user1'>
{
(props) => {
return (
<h2>UserProfile</h2>
)
}
}
{/* <UserProfile /> */}
</Twitter>
</div>
);
}
}
export default App;
class App extends Component {
render() {
const renderContent = params => { params.todos ? <h2>UserProfile</h2> : <div>Loading...</div> }
return (
<div className="App">
<Twitter username='user1' renderContent={ renderContent } />
</div>
);
}
}