How to set the current value of a Ref react-native? - reactjs

I am using a ref to change the value of a text input when a user is typing without unnecessary rerenders. But sometimes, I want to populate the text input first.
Is there a ref.setValue function I can use? It doesn't look like it, from what I've seen of the internal members of the ref, but is there a work around?

I ended up using TextInput's defaultValue prop

Related

Input value not changing when i have preexisting value

I have an ArticleFields component that loads an article object (through location.state) which has either empty parameters(when i try to make a new article) or preexisting values when i want to edit an article.My inputs though, won't change when i pass a preexisting value,even if it's an empty string.On the other hand ,the onChange function still works for textarea.Any ideas to why is this happening,i know i can just change the inputs to textareas,but ofcourse i want to understand why this is happening first
Here is a sandbox for reference
https://codesandbox.io/s/green-sound-sopdvp?file=/src/App.js
Use defaultValue instead of value:
value={article.title}

Set focus to component #2 input field after setting component #1 input field value in ReactJS

ReactJS newbie question. I'm trying to set focus to the "Emergency Service" input field after setting current location on the "Location Lookup" field but both fields live in separate components. I'm already making use of useRef in each component but trying to reference either ref from another component returns an error. Any assistance would be greatly appreciated. Please see my project at link below. I added a comment on line 208 of LocationLookup.js where I'm trying to trigger focus to the Emergency Service field. https://codesandbox.io/s/twilight-waterfall-3okx4?file=/src/index.js
Quick solution would be:
Change EmergencyService - wrap it with React.forwardRef, pass ref to Input (also see how to combine refs (for example https://github.com/gregberge/react-merge-refs)
Create ref (emergencyServiceRef) by useRef() in SearchBar and pass it to EmergencyService.
Pass prop to LocationLookup (maybe onLocationChange)
on line 208, call onLocationChange()
In SearchBar, you would handle callback onLocationChange and do "emergencyServiceRef.current.focus()

material-ui: Autocomplete with value and inputValue stored together

Goal:
It's about the Material Autocomplete from material-ui for React in variant freeSolo. I understand that one is asked to handle value and inputValue independently, but because Formik and Yup are used to save the state and apply validation I would prefer to have only one value outside. For splitting this increases complexity in the outer code noticeably.
Attempt:
https://codesandbox.io/s/autocomplete-with-a-single-state-v442c?file=/demo.tsx
Is an example where I set the prop inputValue to value.title || '' and in onInputChange I check if the inputValue matches an existing option and otherwise create a new object, both to mimic onChange.
Issue:
Unfortunately, the list does not become filtered anymore. I had added some logging in my attempt was everything worked as expected and I can't infer what issue the component runs into. I hope anyone has some idea or ideally working code? So again, my overall goal is to have only one value representing the state and that, therefore, needs to be an object.
So I realised this can not be done because the states value and inputValue update at different times. value is only affected by inputValue once the user clears the input completely, setting inputValue to '' and value becoming null. So the asynchronous nature does not allow storing it in a single value in a clean way.

Use form.getFieldValue to add logic between field without warning

I have a Ant Design 4.x.x Form element without multiple Form.Item. I need to implement some logic involving form items' values, for example disabling a field if another one's value equals something, or recalculate select options when a text input changes.
To do so, I create the form using Form.useForm() and use form.getFieldValue() in my functional component body and / or in the returned JSX, like so :
It is working as I expect to, but at startup, getFieldValue usages throw annoying
index.js:1 Warning: Instance created by `useForm` is not connect to any Form element. Forget to pass `form` prop?
I found that Form functions cannot be used before rendering, and the problem also occured when displaying a form in a Modal like stated in the docs.
So I feel that I'm missing something on how to correctly add custom logic between fields, or doing some calculation involving fields values in component body.
What would be a correct approach to do this ?
Try adding getContainer={false}, to your modal this will work for you.

Can i change input value populated from component state from console

Can i change input value populated from component state from console by using something like document.getElementById('someId').value="some_value" in react
Yes you can.
Whatever the Library/Framework you are using to build your DOM, it will eventually produce DOM, thus, it will be available and accessed as: window, document and everything inside of them.
So, doing the following will update value of the field that matches with the parameter given to the getElementById.
document.getElementById('someId').value="some_value"
I suggest you use React Developer Tools to trace and edit value of state
https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en
Hope this help!

Resources