Comments (3)
Here is the complete code for that file:
import React, { PureComponent } from 'react'
import { compose, withState } from 'proppy'
import { attach } from 'proppy-react'
import { css } from 'emotion'
// import styled from 'react-emotion'
import { Button, TextArea } from '@blueprintjs/core'
import 'normalize.css/normalize.css'
import '@blueprintjs/core/lib/css/blueprint.css'
import '@blueprintjs/icons/lib/css/blueprint-icons.css'
import { produce } from 'immer'
import { Bubble } from './bubble'
const Proppy = compose(
withState('messages', 'setMessages', [])
)
const InputField = attach(Proppy)(class extends PureComponent {
constructor (props) {
super(props)
this.state = {
value: ''
}
}
render () {
const { messages, setMessages } = this.props
return (
<React.Fragment>
<TextArea
large fill
value={this.state.value}
onChange={(e) => {
this.setState({
value: e.target.value
})
}}
/>
<Button text={'ok'} onClick={() => {
setMessages(
produce(messages, draft => {
draft.push(this.state.value)
})
)
this.setState({
value: ''
})
}} />
</React.Fragment>
)
}
})
const ChatWindow = attach(Proppy)((props) => {
const { messages } = props
return (
<div className={css`overflow: auto;`}>
{messages.map((message) => <Bubble message={message} />)
// TODO setMyMessage prop 삽입
}
</div>
)
})
class MyComponent extends PureComponent {
// TODO 이 constructor 없어도 되는거임?
constructor (props) {
super(props)
}
componentDidMount () {
this.props.setMessages(this.props.pastMessages)
// const { setMessages, pastMessages } = this.props
}
render () {
return (
<React.Fragment>
<ChatWindow />
<InputField />
</React.Fragment>
)
}
}
export default attach(Proppy)(MyComponent)
from proppy.
After a couple of experiments, perhaps the problem is that when calling setter() in attach(P)(Comp1), Comp1 is re-rendered, but other attach(P)(Comp2) is not.
Is this the intended behavior of Proppy? Can not multiple components share state through attach?
from proppy.
Hi @YahngSungho,
It is important to understand the difference between Factories and Instances in ProppyJS: https://proppyjs.com/docs/factory-instance/
This is a factory:
const Proppy = compose(
withState('messages', 'setMessages', [])
)
And you attach the Factory to your Component:
export default attach(Proppy)(MyComponent)
Now when the MyComponent
is initialized, it will create an instance out of the ProppyJS factory, and then use it for MyComponent
alone.
Same happens with ChatWindow
component is initialized, and it has its own ProppyJS instance.
Meaning, the ProppyJS instances are not shared between the two components.
That's where Providers come into play: https://proppyjs.com/docs/providers/
They allow you to share common dependencies of your application throughout the whole components-tree.
If you want to share state between components, I recommend using something like a Store, like Redux for example. There is an integration for that here: https://proppyjs.com/docs/packages/proppy-redux/
Full example:
- Setting Redux store as Provider: https://github.com/fahad19/proppy/blob/master/examples/react-playground/src/index.js#L17
- Rest of the app: https://github.com/fahad19/proppy/tree/master/examples/react-playground/src/proppy-redux
from proppy.
Related Issues (20)
- Strange warnings: Cannot find source file HOT 5
- vue-class-component support HOT 4
- Support function version of mapDispatch at proppy-redux withStore HOT 1
- Feature: lifecycle hoc HOT 2
- Missing didUpdate for componentDidUpdate lifecycle function HOT 3
- Proppy-preact doesn't pass children to h() HOT 1
- Drop Babel and ESLint
- parentProps in props (Preact)
- hyperHTML: subscription not working HOT 7
- Testing: how to pass new props when testing factories? HOT 1
- testing: how to test handlers HOT 1
- Proppy subscribers are non removed properly HOT 1
- [FR] Framework dependent intercept function HOT 8
- Consumer used without Provider HOT 2
- Usage with global providers and preact-router HOT 2
- Proposal: React hooks
- unpkg error HOT 2
- drop in recompose replacement? HOT 1
- Return an async iterable HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from proppy.