zoukankan      html  css  js  c++  java
  • [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>
            );
        }
    }
  • 相关阅读:
    USACO 4.1 Fence Rails
    POJ 1742
    LA 2031
    uva 10564
    poj 3686
    LA 3350
    asp.net MVC 3多语言方案--再次写, 配源码
    使用Log4net记录日志
    在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求
    为什么要使用反射机制
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7041502.html
Copyright © 2011-2022 走看看