question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set a default value when loading?

See original GitHub issue

Hi,

I’ve a drop down component which I need to set a default value when the component loads, so I can use that value to call the API first time. How can I achieve this? My code is as follows.

const branches = useSelector(state => state.userData.branches);
  const [open, setOpen] = useState(false);
  const [value, setValue] = useState(null);
  const [items, setItems] = useState([]);

  useFocusEffect(
    React.useCallback(() => {
      const _branch = branches.map(x => {
        return {
          label: x.name,
          value: x.id,
        };
      });
      setItems(_branch);
      getCartData()
    }, []),
  );

async function getCartData() {
   console.log(value) // I need the default value here once the component mounts
  }

<DropDownPicker
        open={open}
        value={value}
        items={items}
        itemKey={item => item.id}
        setOpen={setOpen}
        setValue={setValue}
        setItems={setItems}
        placeholder="Select Branch"
        style={{
          borderColor: 'white',
          marginVertical: '2%',
          shadowColor: '#000',
          shadowOffset: {
            width: 0,
            height: 2,
          },
          shadowOpacity: 0.23,
          shadowRadius: 2.62,

          elevation: 4,
        }}
        dropDownContainerStyle={{
          borderColor: 'grey',
        }}
        />

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
hossein-zarecommented, Feb 23, 2022

@premdasvm Sorry i’m a bit busy. I’ll check it’s behavior.

// faster
const [value, setValue] = useState(id);
3reactions
Blizzardnyacommented, Feb 17, 2022

What about setting a default value in useState? And also in your example there are two “items” states, I guess one of them should be the “open” state?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set default values for fields or controls - Microsoft Support
Set a default value · In the Navigation Pane, right-click the table that you want to change, and then click Design View. ·...
Read more >
3 Ways to Set Default Value in JavaScript | SamanthaMing.com
Let's break down the 3 different ways to set Default Values using logical operator, ternary, and if/else...
Read more >
how to set default value in data loading function? — oracle-tech
I have created a data loading in APEX, and want to record who load the data. so I created one column named user_created....
Read more >
Loading and Applying Default Values - MicroStrategy
To do this, click the Preferences link, then click the Project defaults link on the left-hand side of the page (under the "Preferences...
Read more >
Assigning default index values - IBM
To have the load process assign a default from the Load Information tab or by using propagation, you must specify the _*USELOADDEFAULTORPROPAGATION*_ default...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found