Methods
# bind(bindingProps) → {function}
Returns a bound component, the properties of the bound
component are extended on use
Parameters:
Name | Type | Description |
---|---|---|
bindingProps |
BoundProps | the properties of the binding |
- See:
-
- Bound
a bound component
function
Example
const Input = state.bind({component: <input style={{fontSize: '120%'}} />})
function Example() {
return <div>
<Input property="firstName"/>
<Input property="lastName"/>
</div>
}
# Bind(props) → {function}
A binding target, linking the state to an object
Parameters:
Name | Type | Description |
---|---|---|
props |
BindProps | properties |
the JS component
function
Example
const state = createState("global")
let someState = {id: 1234, name: "Mike"}
function App() {
return <state.Bind target={someState}>
<InnerComponents/>
</state.Bind>
}
# Bound(props) → {function}
Returns a component bound to the state model
Parameters:
Name | Type | Description |
---|---|---|
props |
BoundProps |
a component to be rendered
function
Example
function SubComponent() {
return <div>
<Bound component={<TextField variant="outlined"/>} property="name"/>
</div>
}
# useBinding(property, defaultValueopt, transformInopt, transformOutopt, updateOnBluropt, extractopt, onChange, attributeopt, eventopt, blurEventopt, targetopt) → {object}
Provides a way of creating binding values
for a component
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
property |
string | the property of the current state to bind | ||
defaultValue |
any |
<optional> |
the default value for the property | |
transformIn |
TransformValue |
<optional> |
a function to transform inbound values | |
transformOut |
TransformValue |
<optional> |
a function to transform outbound values | |
updateOnBlur |
any |
<optional> |
set if the component should only update when it blurs | |
extract |
Extractor |
<optional> |
a function that transforms event values to real values - default version will extract from event.target.value if available, otherwise the value itself | |
onChange |
function | |||
attribute |
string |
<optional> |
"value" | the attribute to bind to |
event |
string |
<optional> |
"onChange" | the event to be bound for changes |
blurEvent |
string |
<optional> |
"onBlur" | the event for blurring |
target |
object |
<optional> |
an override for the target |
an object containing the specified value and change function
object
Example
const {props} = state.useBinding()
return <input {...props}/>
# useCalculation(property, dependencies, fn, targetopt)
Creates a calculation that depends on other property values
and is automatically updated when they change
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
property |
string | the property to be created | |
dependencies |
Array.<string> | an array of dependencies for the property, the update function will be passed the values of these | |
fn |
function | a function that will be called with the dependency values and returns the updated calculation | |
target |
any |
<optional> |
an override for the current target (rarely used) |
Example
function Component() {
globalState.useCalculation("profile.fullName", ["profile.firstName", "profile.lastName"],
(firstName, lastName) => `${firstName} ${lastName}`
)
}
function Label({value, ...props}) {
return <div {...props}>{value}</label>
}
const BoundLabel = globalState.bind({component: <Label className={"label"}/> })
function FullNameLabelExample() {
return <Label property="profile.fullName" />
}
# useChange(property, handler, targetopt)
Provides a handler that is called when a property value has been
updated
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
property |
string | the property that is updated | |
handler |
function | the handler for the property change, it will be called with the updated value | |
target |
object |
<optional> |
an optional target override |
Example
import debounce from 'lodash/debounce'
function Component() {
const saveProfile = React.useMemo(()=>debounce(_saveProfile, 300), [])
globalState.useChange("profile.**", saveProfile)
function saveProfile(profile) {
localStorage.setItem("profile", JSON.stringify(profile))
}
}
# useCurrentPath() → {Array.<string>}
Returns the current path of the context
the current path to the bound target
Array.<string>
# useCurrentTarget() → {object}
Returns the current target of the the context
the target
object
Example
const current = state.useCurrentTarget()
const copy = JSON.parse(JSON.stringify(current))
# useRefresh(…paths) → {number}
Causes the caller to refresh if any of the paths change
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
paths |
Array.<string> | string |
<repeatable> |
the paths to refresh on |
the current unique id of the refresh
number
Example
function Component() {
const [style] = globalState.useState("style")
// Update if any sub property of style changes
globalState.useRefresh("style.**")
return <div style={{...style}}>Some Content</div>
}
# useSetter(property, targetopt) → {Setter}
Returns a setter for properties
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
property |
string | the property to set | |
target |
any |
<optional> |
an override for the current value |
- a value to set other values
Example
function Component() {
const setValue = someState.useSetter("some.object.property")
return <Button onClick={clear}>Clear</Button>
function clear() {
setValue({})
}
}
# useState(property, defaultValueopt, targetopt) → {Array}
Provides access to information in the state that will be updated
any time a state change would affect it
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
property |
string | the property path of the state required | |
defaultValue |
any |
<optional> |
a default value for the state |
target |
object |
<optional> |
an override for the standard state |
an array containing the state value and an update function
Array
Example
const [name, setName] = state.useState("person.firstName")
return <div onClick={clearFirstName}>{name}</div>
function clearFirstName() {
setName("")
}