在Downshift Reactjs中保存默认值

时间:2020-06-09 07:50:30

标签: javascript reactjs react-hooks

如何显示默认值,当用户打开它时,它应该显示默认值,用户也可以更改它。我从这里用过 链接-https://scotch.io/tutorials/5-most-common-dropdown-use-cases-solved-with-react-downshift#toc-downshift-with-axios 此代码作为回报在MF.jsx下。

<Downshift
        onChange={downshiftOnChange}
        itemToString={(item) => (item ? item._source.name : "")}
      >
        {({
          selectedItem,
          getInputProps,
          getItemProps,
          highlightedIndex,
          isOpen,
          inputValue,
          getLabelProps,
        }) => (
          <div>
            <label
              style={{ marginTop: "1rem", display: "block" }}
              {...getLabelProps()}
            >
              Mutual Fund scheme Name
            </label>
            <input
              {...getInputProps({
                placeholder: "Enter Mutual Fund Name...",
                onChange: changeInput,
                style: { width: "100%" },
                value: props.values,
              })}
            />
            {isOpen ? (
              <div className="downshift-dropdown">
                {list.map((item, index) => (
                  <div
                    className="dropdown-item"
                    {...getItemProps({ key: index, index, item })}
                    style={{
                      backgroundColor:
                        highlightedIndex === index ? "lightgray" : "white",
                      fontWeight: selectedItem === item ? "bold" : "normal",
                    }}
                  >
                    {item._source.name}
                  </div>
                ))}
              </div>
            ) : null}
          </div>
        )}
      </Downshift>

在输入标签内,在值中我已经传递了props.values,它显示了来自父级的数据,但是,我无法更改输入中的任何内容。如果你知道降档。请告诉我该怎么办? 我想从道具加载数据。而且我也可以更改它。

1 个答案:

答案 0 :(得分:0)

您可以在 getInputProps

中使用 defaultValue 值而不是 value
相关问题