react-native icon indicating copy to clipboard operation
react-native copied to clipboard

How to assign dynamic value to TextInput using react native

Open prashanthmerugucs opened this issue 11 years ago • 8 comments

In my scenario, while focus on TextInput i am moving to another scene using navigator (using push)there i populate list,in that list selecting one value that value should be populated to the previous scene of TextInput In this case I am unable to set the selected value to the previous scene of TextInput.

prashanthmerugucs avatar Oct 23 '15 05:10 prashanthmerugucs

you can use EventEmitter, in previous scene addListener, and in another scene emit the event.

hufeng avatar Oct 23 '15 14:10 hufeng

you could add it to your route object and send it along to the navigator

mangogogos avatar Oct 23 '15 22:10 mangogogos

I am sending the route object through out the navigator,But in my scenario selected value should be reassign to the previous scene of TextInput .Here TextInput event structure is like the below nativeEvent: { target: 2175, pageY: 164.5, locationX: 131, changedTouches: [ [Circular] ], locationY: 16.5, identifier: 1, pageX: 146, touches: [], timestamp: 6887223.188515 }, target: undefined, currentTarget: null, path: undefined, type: undefined, eventPhase: undefined, bubbles: undefined, cancelable: undefined, timeStamp: 1445839347722, defaultPrevented: undefined, isTrusted: undefined, isDefaultPrevented: [Function], isPropagationStopped: [Function], _dispatchListeners: null, _dispatchIDs: null }

Here i am assignnig like
this.nativeEvent.target = "myvalue" this.nativeEvent.target .text= "myvalue" // Here ' this ' is route object Both of this not working .What is the correct way to assign the value to the textinput. The current object getting is ok, my problem is assigning the selected value to the TextInput.

prashanthmerugucs avatar Oct 26 '15 06:10 prashanthmerugucs

The way to assign a value to TextInput is <TextInput value={yourValue} />. This is covered in the TextInput documentation: https://facebook.github.io/react-native/docs/textinput.html#value

In the future please use Stack Overflow to ask questions and tag them with react-native: http://stackoverflow.com/questions/tagged/react-native

Many people from the community hang out on Stack Overflow and will be able to see your question. Using Stack Overflow for questions also helps us use Github issues to keep track of bugs that need to be fixed.

ide avatar Oct 26 '15 06:10 ide

This is static assignment,but i want to set the value dynamically to the TextInput.

Thanks.

prashanthmerugucs avatar Oct 26 '15 07:10 prashanthmerugucs

It's not static: yourValue in <TextInput value={yourValue} /> is a variable.

ide avatar Oct 26 '15 07:10 ide

'yourValue' is a variable ,i am passing that variable through out the navigator ,while selcting the list of value ,that value should be assigned to this variable like yourValue="selectedvalue" this is not happened in my scenario.

prashanthmerugucs avatar Oct 26 '15 09:10 prashanthmerugucs

var FirstComponent = React.createClass({
     render:function(){
             return(<TextInput value="" placeholder="Enter value" onFocus={getData.bind(this)} />)
      }
})

function getData(ev){
var target = ev;
      this.props.navigator.push({
          id:'SecondComponent',
          name:'SecondComponent'',
          target:target, 
       })
}
var SecondComponent = React.createClass({
        render:function(){
               return(<TouchableHighlight onPress={fillData.bind(this,target, self.props.navigator,selectedText)}><Text>sample</Text></TouchableHighlight>)
        }
});
function fillData(target,nav,selectedText,ev){
     target =selectedText;
//Here target is root component this
//how to fill selectedText in FirstComponent TextInput value
}

prashanthmerugucs avatar Oct 26 '15 10:10 prashanthmerugucs