[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> ); } }
相关文章推荐
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- FW:Use the OnReadyStateChange Property in Visual Basic and Visual C/C++
- Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
- 海洋工作室——网站建设专家:The version of SQL Server in use does not support datatype datetime2 and the Entity Framework.
- it has a DefiningQuery and no InsertFunction element exists in the ModificationFunctionMapping element to support the current op
- use MyButton is simple,only add two file and then add some code just bellow in any dlg src declear the button control and will ok!
- const at the end of function and passing const object in a function call
- Can we use function on left side of an expression in C and C++?
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- Item 16: Use the same form in corresponding uses of new and delete.(Effective C++)
- How and Why to use the System.servicemodel.MessageParameterAttribute in WCF
- Use the unsinged type as operands of shift operator in C language
- Use the edge function in matlab
- [C++0x] Boost Thread in Vector and use lamda as thread function
- How to use the pointer of function in a class?
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- ADF Faces RC - How-to use the Client and Server Listener Component
- TRIGGERS :Cannot use text, ntext, or image columns in the 'inserted' and ' deleted' tables.
- wpf - ComponentResourceKey and it use in Author...
- Internet Information Services is not installed. You must have Internet Information Services installed in order to use the SharePoint Products and Technologies Configuration Wizard