您的位置:首页 > Web前端 > React

[Preact] Use State and Props in the Component Render Function

2017-06-17 22:01 441 查看
Preact offers, in addition to the regular component API from React, the ability to access both props & state as function parameters to the render method. This lesson will cover an example of how to utilize this convenience along with how destructuring can make it even nicer to work with.

import {h, Component} from 'preact';
import User from './User';

export default class App extends Component {
constructor(props) {
super(props);

this.state = {
loading: true,
user: null
};
}

componentDidMount() {
fetch(this.props.config.urls.user)
.then(resp => resp.json())
.then(user => {
this.setState({
user,
loading: false
});
})
.catch(err => console.error(err));
}

// render(props, state) {
render({config}, {loading, user}) {
return (
<div class="app">
{loading
? <p>Fetching {config.urls.user}</p>
: <User image={user.avatar_url}
name={user.name} />
}
</div>
);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐