[Preact] Use State and Props in the Component Render Function
Posted Answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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的主要内容,如果未能解决你的问题,请参考以下文章
[Mobx] Use MobX actions to change and guard state