admin管理员组文章数量:1391951
I'm creating form in React Native and Redux. I added TextInput
and want to change the state of this field in Reducer. I have a trouble, because I don't know how to add (change) => this.setState({change})
function to my Redux architecture. I use bine reducers and have no idea how to bind that value.
Saying in short, I need to gain default behaviour of the TextInput
on change function but I have to use Redux to do it.
Form.js
const mapDispatchToProps = dispatch => {
return {
changeNameInput: () => dispatch({type: 'CHANGE_NAME_INPUT'})
};
};
const mapStateToProps = state => {
return {
change: state.changeNameInput.change
};
};
class Form extends React.Component {
render() {
return (
<View>
<FormLabel>Name</FormLabel>
<TextInput
onChangeText={this.props.changeNameInput}
value={this.props.change}
/>
</View>
);
}
}
Reducer.js
const initialState = {
change: 'Your name'
};
const changeinputReducer = (state = initialState, action) => {
switch (action.type) {
case 'CHANGE_NAME_INPUT':
//Problem
return {...state, change: '????' };
default:
return state;
}
};
export default changeinputReducer;
I'm creating form in React Native and Redux. I added TextInput
and want to change the state of this field in Reducer. I have a trouble, because I don't know how to add (change) => this.setState({change})
function to my Redux architecture. I use bine reducers and have no idea how to bind that value.
Saying in short, I need to gain default behaviour of the TextInput
on change function but I have to use Redux to do it.
Form.js
const mapDispatchToProps = dispatch => {
return {
changeNameInput: () => dispatch({type: 'CHANGE_NAME_INPUT'})
};
};
const mapStateToProps = state => {
return {
change: state.changeNameInput.change
};
};
class Form extends React.Component {
render() {
return (
<View>
<FormLabel>Name</FormLabel>
<TextInput
onChangeText={this.props.changeNameInput}
value={this.props.change}
/>
</View>
);
}
}
Reducer.js
const initialState = {
change: 'Your name'
};
const changeinputReducer = (state = initialState, action) => {
switch (action.type) {
case 'CHANGE_NAME_INPUT':
//Problem
return {...state, change: '????' };
default:
return state;
}
};
export default changeinputReducer;
Share
Improve this question
asked Feb 9, 2018 at 14:10
ItalikItalik
6963 gold badges20 silver badges37 bronze badges
1 Answer
Reset to default 7For passing value from TextInput to reducer, you need to change in dispatch function:
const mapDispatchToProps = dispatch => {
return {
changeNameInput: (text) => dispatch({type: 'CHANGE_NAME_INPUT', text})
};
};
and in your reducer change it to
const changeinputReducer = (state = initialState, action) => {
switch (action.type) {
case 'CHANGE_NAME_INPUT':
//Solution
return {...state, change: action.text };
default:
return state;
}
};
Hope it worked..
本文标签: javascriptBind text input value on change eventReact NativeReduxStack Overflow
版权声明:本文标题:javascript - Bind text input value on change event - React Native + Redux - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744691984a2620050.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论