Use Transis and React in ES6
const Person = Transis.Object.extend(function() {
this.prop('name')
})
const Car = Transis.Object.extend(function() {
this.prop('name')
})
window.appState = new GlobalTransisObject({
person: new Person({ name: 'john' })
})
import createClass from 'create-react-class'
import { StateMixin, PropsMixin } from 'transis-react'
const Classic = createClass({
mixins: [
StateMixin(appState, {
person: ['name']
}),
PropsMixin({
car: ['name']
})
],
render() {
return <div>
name: {this.state.person.name}
drives: {this.props.car.name}
</div>
} // outputs "name: john, drives: Accord"
})
mount(<Classic car={new Car({ name: 'Accord' })}>)
import transisReact from 'transis-react'
// stateless!
const MyComponent = ({ person, car }) =>
<div>
name: {person.name}
drives: {car.name}
</div>
// or it can be ES6, where React lifecycle callbacks are available
class MyComponent extends Component {
render = () =>
<div>
name: {this.props.person.name}
drives: {this.props.car.name}
</div>
}
export default transisReact({
global: appState,
state: {
person: ['name']
},
props: {
car: ['name']
}
}, MyComponent)
Occasionally you might have a prop that have the same name that conflicts with the glboal state name, in this case you can use remap
config:
const MyComponent = ({ person, car }) =>
<div>
name: {person.name}
drives: {car.name}
</div>
export default transisReact({
global: appState.person,
state: {
name: []
},
remap: {
name: 'personName'
}
}, ({ name, personName }) =>
<div>
name: {personName}
drives: {name}
</div>
)
The wrapper HigherOrderComponent
can access the actual component with a #core
ref
const component = mount(<MyComponent />)
component.node.state // { person: <TransisObject>{ name: 'john' } }
component.node.state.person === component.node.core.props.person // same
- Global Mixined
state
variables are now bound toprops
on the CoreComponent. They are kept bound tostate
on the wrapper for change detection purposes. componentWillReceiveProps
is triggered whenever Global Mixined state are updated in any way.- lifecycle events that used to listen to
state
variable that was mixined throughglobal appState
will now need to listen for `props, such as
shouldComponentUpdate({nextProps}, {nextState})
componentWillUpdate({nextProps}, {nextState})
componentDidUpdate({preivousProps}, {previousState})