-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] DI JSX-

, 25 2017 . 14:24 +
redyuf 14:24

DI JSX-

Dependency inception


, . , SOLID , , , DI.


DI , , . , c react- .


DI


, react. , connect redux inject mobx-react. , (MessageList) , (Button) , .


const PropTypes = require('prop-types');

const Button = ({children}, context) =>
  ;

Button.contextTypes = {color: PropTypes.string};

class MessageList extends React.Component {
  getChildContext() {
    return {color: "purple"};
  }

  render() {
    return ;
  }
}

.. context.color, , contextTypes color. Button . getChildContext() ... color .


, . , color . Button , .


, , , . :


It is an experimental API and it is likely to break in future releases of React

. experimental , . (getChildContext), PropTypes service locator, . , , : , redux mobx.


. , vue: provide/inject, angular, angular di DI typescript. -, Angular , ( DI ) . , ?


,


/redux- redux-. - setState. redux , setState , , .. . You Might Not Need Redux , " redux", . , , .


, Presentational and Container Components, , " (Presentational), (Container)" ( redux, mobx, relay, setState). Container , .


- Container-, HOC, - . connect/inject - redux, mobx, relay. Container .


, Presentational Container, . .. , , . , . , /.


, SOLID, , . , - - . .. .


> this.setState({ dialogOpen: false })} />
    
        
            
                > this.setState({ dialogOpen: false })} />
                Dialog header
            
            Some content
            
                
                
            
        
    

, , - , Button, . SOLID . - , - .



, . :


// @flow

// @jsx lom_h

// setup...

class HelloService {
    @mem name = ''
}

function HelloView(props: {greet: string}, service: HelloService) {
    return 
{props.greet}, {service.name}
> { service.name = e.target.value }} />
} // HelloView.deps = [HelloService] ReactDOM.render(, document.getElementById('mount'))

fiddle


, , . . babel-plugin-transform-metadata. typescript, , , . : HelloView.deps = [HelloService]


Lifecycle


? ? HOC lifecycle methods , , relay/graphql.


, . (, lazyObservable mobx-utils), componentDidMount . jquery-, refs ..


, , vendor lock-in , . , . , , . HelloService -.


,


, , , . , , : , JSX.


, , . ( /), , ( ).


, DI , . .. , . UI :


class MyPanel extends React.Component {
    header() { return 
{this.props.head}
} bodier() { return
{this.props.body}
} childs() { return [ this.header() , this.bodier() ] } render() { return
{this.childs()}
}

class MyPanelExt extends MyPanel {
    footer() { return 
{this.props.foot}
} childs() { return [ this.header() , this.bodier() , this.footer() ] } }

, (@vintage), tree, . , , . , (, ) O SOLID.


JSX , DI . , , , vue. , ( ). , .


js/ts, runtime, , , . flow typescript ( C# Dart):


interface ISome {}

class MySome implements ISome {}

const map = new Map()
map.set(ISome, MySome)

, .


class AbstractSome {}

class MySome extends AbstractSome {}

const map = new Map()
map.set(AbstractSome, MySome)

.. DI-, map, . .. , , , .


, TodoResetButtonView TodoView. TodoResetButtonView .


function TodoResetButtonView({onClick}) {
  return 
}

function TodoView({todo, desc, reset}) {
    return 
  • > todo.finished = !todo.finished} />{todo.title} #{todo.id} ({desc.title}) reset
  • }

    TodoView ( , open/close 11 , ).


    TodoView, . , TodoView , .


    function ClonedTodoResetButtonView({onClick}) {
      return 
    }
    
    const ClonedTodoView = cloneComponent(TodoView, [
        [TodoResetButtonView, ClonedTodoResetButtonView]
    ], 'ClonedTodoView')
    
    const ClonedTodoListView = cloneComponent(TodoListView, [
        [TodoView, ClonedTodoView]
    ], 'ClonedTodoListView')
    
    ReactDOM.render(, document.getElementById('mount'));

    fiddle


    , :


    class AbstractHelloService {
        name: string
    }
    
    function HelloView(props: {greet: string}, service: AbstractHelloService) {
        return 
    {props.greet}, {service.name}
    > { service.name = e.target.value }} />
    } class AppHelloService { @mem name = 'Jonny' } function AppView() { return } AppView.aliases = [ [AbstractHelloService, AppHelloService] ]

    fiddle


    HelloView AppHelloService. .. AppView.aliases AbstractHelloService.


    " " . .. , , , , . "", , "", , .. ( LSP ).



    -, . : . .. , , , .


    class HelloService {
        @mem name = 'John'
    }
    
    function HelloView(props: {greet: string}, service: HelloService) {
        return 
    {props.greet}, {service.name}
    > { service.name = e.target.value }} />
    } class AppHelloService { @mem name = 'Jonny' } function AppView(_, service: HelloService) { return
    }

    fiddle


    , HelloView HelloService. , HelloService AppView, .


    function AppView() {
        return 
    }

    , , , DI .



    , css-in-js . . redux/mobx . , , jss injectSheet , react-jss:


    import React from 'react'
    import injectSheet from 'react-jss'
    
    const styles = {
      button: {
        background: props => props.color
      },
      label: {
        fontWeight: 'bold'
      }
    }
    
    const Button = ({classes, children}) => (
      
    )
    
    export default injectSheet(styles)(Button)

    , jss , DI. , .


    // ... setup
    import {action, props, mem} from 'lom_atom'
    import type {NamesOf} from 'lom_atom'
    
    class Store {
      @mem red = 140
    }
    
    function HelloTheme(store: Store) {
      return {
        wrapper: {
          background: `rgb(${store.red}, 0, 0)`
        }
      }
    }
    HelloTheme.theme = true
    
    function HelloView(
      _,
      {store, theme}: {
        store: Store,
        theme: NameOf
      }
    ) {
      return 
    color via css {store.red}: > { store.red = Number(target.value) }} />
    }

    fiddle


    DI, . css, flow/ts. css.



    , reactive-di. , , , , .. todomvc react, preact, inferno. reactive-di. , 100 todos, , .


    Angular. , reactive-di


    1. , legacy-
    2. , mobx/observe
    3. flowtype , -
    4. : , react, ,
    5. , , provide/inject-
    6. , c
    7. , , css-in-js

    ? DI flow/ts . backend- ( InversifyJS Ninject C#) . : , DI react vue, .


    DI . @mem, , . mem , , - mobx, . .

    Original source: habrahabr.ru (comments, light).

    https://habrahabr.ru/post/338666/

    :  

    : [1] []
     

    :
    : 

    : ( )

    :

      URL