2017-03-15 106 views
0

来支撑我的新反应,终极版,我试图用它可以传递给onChange事件保存到道具,从一个反应 - 终极版形式的文本框中输入的值为文本框的另一个组件阵营 - 终极版表格保存数据来自的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 /主/ src目录/组件/ 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); 

这是我刚输入的内容。不确定它是否可行,但它涵盖了动作,缩减器以及从一个组件更新值到anohter。当然,这是一种使用反应减少的疯狂方式。我不确定在每次输入更改时调用此操作的效率如何。将状态的当前值作为道具传递给所需的组件可能会更好。

如果您还有其他问题,我很乐意为您提供帮助或指引您使用其他资源。

+0

谢谢,但我想知道是否值除了两个组件之间共享之外没有消失它需要通过减速器?状态是否可以在没有太多脚本的情况下进行更新?此外,文本框已经是表单的一部分,并不是一个组件,它的组件有一个连接 – Andy5

+0

是的,它可以发生。让我在这里掀起一个codepen来演示它.... –

+0

谢谢,这将是最有帮助的 – Andy5