1
你好我是新來的,在反應原生初學者我試圖做一個textInput和,我想通過數據(文本輸入用戶)在其他屏幕上我怎麼能做到這一點你能幫助我嗎?我使用的反應導航StackNavigator使用反應原生導航與堆棧導航器將數據從屏幕傳遞到其他屏幕
這裏一些我的代碼,我的第一類具有TextInput
export default class ParamScreen extends React.Component {
static navigationOptions = {
title: ' Covoit Ulg ',
headerStyle:{ backgroundColor: '#66CDAA'},
headerTitleStyle:{ color: '#FFF', alignSelf: 'center'},
titleStyle: {alignSelf: 'center'}
};
render() {
const { navigate } = this.props.navigation;
return (
<View>
<InputControl/>
<Button onPress = {() => navigate('ParamPass',{TextInput: ''})}
kind = 'rounded'
type = 'danger'
style={btnStyle}
marginBottom= '10'>pass data ?</Button>
</View>
)
}
}
class InputControl extends React.Component {
constructor(props) {
super(props)
this.handleTextInput = this.handleTextInput.bind(this)
this.state = {textIn: false}
}
handleTextInput() {
this.setState({textIn: true})
}
render() {
const textIn = this.state.textIn
let TextInput = null
if (textIn) {
TextInput = <UserInput onSelectionChange={this.handleTextInput}/>
}
return (
<View>
<UserInput textIn={textIn}/>
{TextInput}
</View>
)
}
}
function UserInput(props) {
return <TextInput onSelectionChange={props.onSelectionChange}>
Test data test data
</TextInput>
}
而這正是我希望我的數據
export default class ParamsData extends React.Component {
render(){
const {state} = this.props.navigation;
console.log("test test" ,this.props.navigation)
var textIn = state.params ? state.params.textIn : "<undefined>";
return (
<View>
<Text>Second View: {textIn}</Text>
</View>
)
}
}
感謝因爲你的幫助和抱歉我的英語不是最好的,但我正在努力^^(更好的說話然後寫作)``
export default class ParamScreen extends React.Component {
constructor(props) {
super(props)
this.state = { text: 'Test test'}
}
static navigationOptions = {
title: ' Covoit Ulg ',
headerStyle:{ backgroundColor: '#66CDAA'},
headerTitleStyle:{ color: '#FFF', alignSelf: 'center'},
titleStyle: {alignSelf: 'center'}
};
onSubmit =() => {
//whatever you want to do on submit
this.props.navigation.navigate('Parampass', {text: this.state.text})
}
render() {
const { navigate } = this.props.navigation;
return (
<View>
<TextInput style={style.input}
underlineColorAndroid= 'transparent'
onChangeText= { (text) => this.setState({text})}
value= {this.state.text}
/>
<Button onPress = {() =>this.onSubmit()}
kind = 'rounded'
type = 'danger'
style={btnStyle}
marginBottom= '10'>pass data ?</Button>
</View>
)
}
}
我試着像這樣在第一屏
export default class ParamsData extends React.Component {
static navigationOptions = ({navigation}) => {
return {
title: 'Test/${navigation.state.params.text}'
}
}
constructor (props) {
super(props)
this.state = {
text: this.props.navigation.state.params.text,
report: null
}
}
render(){
return (
<View>
<Text>Second View: {text}</Text>
</View>
)
}
}
,這是我收到的數據幫助我,請我覺得我很接近
謝謝你的答案,但你可以使它在我的代碼,如果你需要的東西只是問謝謝:) – NoOne