2017-03-15 3 views
0

새로운 react-redux이고 전달할 수있는 onChange 이벤트를 사용하여 react-redux-form 텍스트 상자에서 입력 한 값을 소품에 저장하려고합니다. 나는이 값을 저장하고 다른 구성 요소이 제공 할 수있는 방법 텍스트 상자의 또 다른 구성 요소React-Redux-form onChange에서 소품 데이터 저장

내 코드는

<ListItemContent> 
     <Control component={Textfield} model="somemodel" label="MyLabel" 
     onChange={this.props}/> 
</ListItemContent> 

입니까?

편집 나는 그것이 부분적으로 일하고 있습니다

 <ListItemContent> 
      <Control component={Textfield} model="somemodel" label="MyLabel" 
      onBlur={this.onChangeOfValue}/> 
    </ListItemContent> 

     onChangeOfValue = (event) => 
     { 
      this.setState({ newValueToPassAlong: event.target.value}); //newValueToPassAlong is set in constructor 
    }; 



    ..... 

    let mapStateToProps = (state) => { 
    return {newValueToGive: state.newValueToPassAlong} //This is undefined 
    }; 

    export default connect(mapStateToProps)(form) 

은 또한, 내 componentWillReceiveProps (nextProps)이 때 다른 구성 요소의 상태 변경을 해고되지 않습니다.

+0

반응과 redux 정보를 전달할 수있는 많은 것들이 있습니다. redux를 사용하여 입력 필드의 상태를 업데이트하고 다른 구성 요소에서 mapStateToProps를 계획하려는 경우 react-redux-form을 사용하는 기본 react-redux 앱의 예를 검토 할 수 있습니다. https : // github .com/Fallenstedt/redux_blog/blob/master/src/components/posts_new.js # L24 자세한 정보를 제공하기 전에이 정보를 다른 구성 요소로 전달하려는 계획과 관련하여 추가 정보가 필요합니다. –

+1

우리는 두 가지 구성 요소를 가지고 있습니다 - 하나는 입력 양식이고 하나는 텍스트 상자에서 onChange를 수행해야하고 다른 하나는이 텍스트 상자에서 값을 전달해야하는 보조 양식입니다. 양식 제출이 없으며 전 세계적으로이 값에 액세스 할 수있는 방법이 있는지 여부는 확실하지 않습니다. – Andy5

답변

0
// YOUR TEXTFIELD COMPONENT 
import React, { Component } form 'react'; 
import { reduxForm, Field } from 'redux-form'; 
import {passValueToOtherComponent} from '../actions/your-actions-index-file'; 
import { connect } from 'react-redux'; 
import ListItemContent form 'list-item-content'; 

class TextField extends Component { 
    constructor(props) { 
    super(props); 

    this.state = { 
     textFieldValue: '', 
    } 
    this.onInputChange = this.onInputChange.bind(this); 
    } 

    onInputChange(event) { 
    var newValue = event.target.value; 
    this.setState({textFieldValue: newValue}); 
    //when input changes 
    //call action to update global state... 
    this.props.passValueToOtherComponent(this.state.textFieldValue) 
    } 

    render() { 
    <form> 
    <ListItemContent> 
     <Control component={Textfield} model="somemodel" label="MyLabel" 
      onChange={this.onInputChange} value={this.state.textFieldValue}/> 
    </ListItemContent> 
    </form> 
    } 
} 

//ReduxForm wrapper 
const wrappedReduxForm = connect(null, {passValueToOtherComponent})(TextField) 

export default reduxForm({ 
    form: 'TextField' 
})(TextField) 


// actions/your-actions-index-file.js 
//create an action which will call to update global state 
export const NEW_VALUE = "NEW_VALUE" 

export function passValueToOtherComponent(value) { 
    return { 
    type: CREATE_POST, 
    payload: value, 
    } 
} 


//YOUR NewValue Reducer 
//reducer_new_value.js 
//create reducer which will accept payload data 
import {NEW_VALUE} from '../actions/your-actions-index-file'; 

const INITIAL_STATE = { 
    valueToPass: null 
}; 

export default function (state = [], action) { 
    console.log('Action...' action); 

    switch (action.type) { 
    case NEW_VALUE: 
     return { ...state, valueToPass: action.payload.data} 
     break; 
    default: 

    } 
} 

//Your Root Reducer 
//Because you may have lots of state to manage, a rootReducer is awesome in managing it all 
import { combineReducers } from 'redux'; 
import NewValueReducer from './reducer_new_value'; 

const rootReducer = combineReducers({ 
    // state: (state = {}) => state 
    value: NewValueReducer, 
}); 

export default rootReducer; 



//finally pass this desired value to your desired Component 
import React, {Component} from 'react'; 
import { connect } from 'react-redux'; 

class OtherComponent extends Component { 

    render() { 
    return (
     <div> 
     <input value= {this.props.texFieldValue}> 
     </div> 
    ) 
    } 
} 


function mapStateToProps(state) { 
    return { textFieldValue: state.value.valueToPass } 
} 

export default connect(mapStateToProps)(OtherComponent); 

이것은 방금 입력 한 것입니다. 작동 여부는 확실치 않지만 조치, 감속기 및 하나의 구성 요소에서 값을 업데이트하는 것을 포함합니다. 물론, 이것은 reactx를 사용하여 그것을하는 미친 방법입니다. 모든 입력 변경시이 액션을 호출하는 것이 얼마나 효율적인지 확신 할 수 없습니다. 현재 상태 값을 원하는 구성 요소로 전달하는 것이 더 나을 것입니다.

더 궁금한 점이 있으면 기꺼이 도와 주거나 다른 리소스를 알려 드리겠습니다.

+0

고맙지 만 두 가지 구성 요소간에 공유되는 가치가 떨어져 있지는 않은지 궁금합니다. 감속기를 통과해야합니까? 너무 많은 스크립팅없이 상태를 간단히 업데이트 할 수 있습니까? 또한 텍스트 상자는 이미 양식의 일부이며 자체적으로 구성 요소가 아니며 구성 요소에는 연결이 있습니다. – Andy5

+0

그래, 일어날 수 있습니다. 저에게 그것을 보여주기 위해 코덱을 채찍질합시다. –

+0

고마워요. 가장 도움이 될 것 같네요. – Andy5