![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-inject-promise
Advanced tools
Inject a promised value into a react component, either by 'higher order component' or by 'function as children'.
Hocs which resolve and inject promises.
npm install react-inject-promise
or yarn add react-inject-promise
type ReactComponent<Props> = React.StatelessComponent<Props>|React.ComponentClass<Props>;
// A hoc(higher-order-component)
injectPromise<Props>(arg:{
values:{
[name:string]:(props:Props)=>Promise<any>
},
shouldReload:(p1:Props,p2:Props)=>boolean
})(Component:ReactComponent<Props>)
Inject the promise(s), the injected props are:
Render a function as children, the argument passed to that function is similar to 'injectPromise':
import * as React from "react"
import {injectPromise} from "react-inject-promise"
@injectPromise({
values:{
users:(props)=>fetch(`/users?group=${props.group}`).then(r=>r.json()),
//todos: ...
},
shouldReload=(props,nextProps)=>props.group!==nextProps.group
})
class Haha extends React.PureComponent<any,any>{
render(){
const {
users, // undefined or the resolved value
reloadUsers, // a callback to re-resolve the promise
setUsers, // set the resolved value manually
usersLoading // whether the promise is not resolved yet.
} = this.props
if(usersLoading)
return <div>loading...</div>
return <div>
{
JSON.stringify(users)
}
<button onClick={reloadUsers}>Reload Users</button>
<button onClick={()=>{
fetch(`addUser`,{
body:JSON.stringify({name:'Shane'})
}).then((newUser)=>{
setUsers(users.concat(newUser))
//or reloadUser()
})
}}>Add User</button>
</div>
}
}
import * as React from "react"
import {RenderPromise} from "react-inject-promise"
const fetchUsers = (groupId)=>fetch(`/users?group=${groupId}`).then(r=>r.json())
function Component(props){
/*
* when reloadFlag changes, the 'promise' prop will be re-called, e.g. reload data from remote API.
* it is ok to pass an inline function as 'promise' props, because RenderPromise will only use the first 'promise' prop it sees, except when reloadFlag has changed.
*/
return <RenderPromise promise={()=>fetchUsers(props.groupId)} reloadFlag={props.groupId}>
{({
value,
valueLoading,
setValue,
reloadValue
})=>{
return valueLoading?"loading...":<p>{value}</p>
}}
</RenderPromise>
}
FAQs
Inject a promised value into a react component, either by 'higher order component' or by 'function as children'.
The npm package react-inject-promise receives a total of 3 weekly downloads. As such, react-inject-promise popularity was classified as not popular.
We found that react-inject-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.