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.

[Select] Component not displaying the value on the first mount

See original GitHub issue
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When using the Select component it does not displaying the value on the first mount after data is receive from API mui select

Expected Behavior 🤔

Render the value on the first mount like the other component (textfield) after data is receive from API

Steps to Reproduce 🕹

Here is my code

const [pile, setPile] = useState([]);

//Fetch
useEffect(() => {
    if (open) {
      const pileRequest = axios.get(`${process.env.REACT_APP_API_URL}/pile/${idPile}`);
      const typeRequest = axios.get(`${process.env.REACT_APP_API_URL}/type/name`);
      Promise.all([pileRequest, typeRequest])
        .then((result) => {
          setPile(result[0].data);
          setType(result[1].data);
        }).catch((err) => {
          console.log(err);
        });
    }
  }, [open]);

***

// Select
<FormControl fullWidth margin="normal">
    <InputLabel>Type</InputLabel>
    <Select label="Type" onChange={handleChangeType} value={pile.type}>
        <MenuItem value={0}>Board</MenuItem>
        <MenuItem value={1}>Panel</MenuItem>
    </Select>
</FormControl>

Context 🔦

I want to display the values from my API with pre-filled fields

Your Environment 🌎

`npx @mui/envinfo`
  System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
  Binaries:
    Node: 14.17.6 - ~/.nvm/versions/node/v14.17.6/bin/node
    Yarn: Not Found
    npm: 7.23.0 - ~/.nvm/versions/node/v14.17.6/bin/npm
  Browsers:
    Chrome: Not Found
    Firefox: Not Found
  npmPackages:
    @emotion/react: ^11.4.1 => 11.4.1 
    @emotion/styled: ^11.3.0 => 11.3.0 
    @mui/core:  5.0.0-alpha.49 
    @mui/icons-material: ^5.0.1 => 5.0.1 
    @mui/lab: ^5.0.0-alpha.48 => 5.0.0-alpha.49 
    @mui/material: ^5.0.1 => 5.0.2 
    @mui/private-theming:  5.0.1 
    @mui/styled-engine:  5.0.1 
    @mui/styles: ^5.0.1 => 5.0.1 
    @mui/system:  5.0.2 
    @mui/types:  7.0.0 
    @mui/utils:  5.0.1 
    @types/react:  17.0.24 
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    typescript:  4.4.3
Browser `Microsoft Edge`

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MOHAMMADArsalancommented, Oct 4, 2021

@MGMehdi the issue this your are using const [pile, setPile] = useState([]); as an array and passing to select pile.type in this case type is undefined. So that’s why value is not showing. try to change state like

- const [pile, setPile] = useState([]);
+ const [pile, setPile] = useState({type: 0})
0reactions
mnajdovacommented, Oct 5, 2021

It doesn’t look like this bug report has enough info for one of us to reproduce it.

Please provide a CodeSandbox (https://material-ui.com/r/issue-template-latest), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem.

Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-select does not show the selected value in the field
The values when selected are stored in the state via portfolioSelector function. The problem is that when i select a value it wasn't...
Read more >
Selected value not showing in the input field [Controlled ...
Same issue here. The workaround works but it is a lot of code for a simple select component with a few elements in...
Read more >
API - React Select
The statemanager is a utility class that wraps around the base Select and each Select variant to expose inputValue and value as controllable...
Read more >
Docs • Svelte
Complete documentation for Svelte.
Read more >
React.Component
React lets you define components as classes or functions. ... extra re-rendering which, while not visible to the user, can affect the component...
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