首页 > 代码库 > [Preact] Use State and Props in the Component Render Function
[Preact] Use State and Props in the Component Render Function
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> ); }}
[Preact] Use State and Props in the Component Render Function
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。