-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cmf): add two state static accessors #1371
Changes from 9 commits
78a01f6
965e559
1d195e4
8e1710a
19196cf
0ded002
8128f97
a66aeb8
7f869b0
d8ad60b
54d037c
e93c552
bacf253
835c1e4
6e4c1fb
583f772
be473e0
6516c58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes'; | |
import { connect } from 'react-redux'; | ||
import omit from 'lodash/omit'; | ||
import api from './api'; | ||
import actions from './actions'; | ||
import deprecated from './deprecated'; | ||
import onEvent from './onEvent'; | ||
import { initState, getStateAccessors, getStateProps } from './componentState'; | ||
|
@@ -245,6 +246,21 @@ export default function cmfConnect({ | |
router: PropTypes.object, | ||
}; | ||
static WrappedComponent = WrappedComponent; | ||
static getState = function getState(state, id = 'default') { | ||
return state.cmf.components.getIn([getComponentName(WrappedComponent), id], defaultState); | ||
}; | ||
static setStateAction = function setStateAction(state, id = 'default', type) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. state parameter to be renamed newState i think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newState already exists in the upper declaration |
||
return { | ||
type: type || `${getComponentName(WrappedComponent)}.setState`, | ||
cmf: { | ||
componentState: actions.components.mergeState( | ||
getComponentName(WrappedComponent), | ||
id, | ||
state, | ||
), | ||
}, | ||
}; | ||
}; | ||
|
||
constructor(props, context) { | ||
super(props, context); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,6 +221,32 @@ If you want to render some component conditionally, just pass "renderIf" prop (t | |
You can also use Expression for this and customize this prop like "renderIfExpression" in | ||
CMF json configuration files | ||
|
||
## How to access state and mutate from the outside | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. title may be misleading, the state is not mutated in sense of pointer value mutation, but it is updated |
||
|
||
Every cmfConnected component expose two static function: | ||
* getState | ||
* setStateAction | ||
|
||
So if we take back the `Clock` example from below and we try to write a saga: | ||
|
||
```javascript | ||
import Clock from './Clock.connect'; | ||
|
||
export default function* myDeLorean() { | ||
const clockState = yield select(Clock.getState); | ||
const action = Clock.setStateAction(clockState.set('date', new Date('2025/12/25'))); | ||
yield put(action); | ||
} | ||
``` | ||
|
||
If you have multiple instance of the same component those api support `id` as a second argument. | ||
|
||
```javascript | ||
const componentState = Clock.getState(state, 'a-component-id'); | ||
// mutation | ||
Clock.setStateAction(componentState, 'a-component-id'); | ||
``` | ||
|
||
## How to test | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm some cleaning maybe ;)