Coder Social home page Coder Social logo

Comments (3)

YahngSungho avatar YahngSungho commented on May 16, 2024

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.

YahngSungho avatar YahngSungho commented on May 16, 2024

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.

fahad19 avatar fahad19 commented on May 16, 2024

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:

from proppy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.