简体   繁体   English

使用钩子和 redux 材料 ui 将值设置为文本字段

[英]Set value to textfield with hooks and redux material ui

I'm building an app using react, redux, and redux-saga.我正在使用 react、redux 和 redux-saga 构建应用程序。 The situation is that I'm getting information from an API.情况是我从 API 获取信息。 In this case, I'm getting the information about a movie, and I will update this information using a basic form.在这种情况下,我正在获取有关电影的信息,并且我将使用基本表单更新此信息。 What I would like to have in my text fields is the value from the object of the movie that I'm calling form the DB.我想在我的文本字段中包含我从数据库调用的电影的 object 的值。 This is a brief part of my code: Im using 'name' as an example.这是我的代码的一个简短部分:我以“名称”为例。 Parent component:父组件:

 const MovieForm = (props) => {
  const {
    movie,
  } = props;

  const [name, setName] = useState('');

  const handleSubmit = (e) => {
    e.preventDefault();

    onSubmit({
      name,
    });
  };

  const handleSetValues = () => {
    console.log('hi');
    console.log(movie, name);
    setName(movie.name);
    setValues(true);
  };

  useEffect(() => {
    if (movie && values === false) {
      handleSetValues();
    }
  });

  return (
    <Container>
      <TextField
        required
        **defaultValue={() => {
          console.log(movie, name);
          return movie ? movie.name : name;
        }}**
        label='Movie Title'
        onChange={(e) => setName(e.target.value)}
      />
    </Container>
  );
};

export default MovieForm;
    ....

child component子组件

const MovieUpdate = (props) => {
  const { history } = props;
  const { id } = props.match.params;

  const dispatch = useDispatch();
  const loading = useSelector((state) => _.get(state, 'MovieUpdate.loading'));
  const created = useSelector((state) => _.get(state, 'MovieUpdate.created'));
  const loadingFetch = useSelector((state) =>
    _.get(state, 'MovieById.loading')
  );
  const movie = useSelector((state) => _.get(state, 'MovieById.results'));

  useEffect(() => {
    if (loading === false && created === true) {
      dispatch({
        type: MOVIE_UPDATE_RESET,
      });
    }
    if (loadingFetch === false && movie === null) {
      dispatch({
        type: MOVIE_GET_BY_ID_STARTED,
        payload: id,
      });
    }
  });

  const updateMovie = (_movie) => {
    const _id = id;
    const obj = {
      id: _id,
      name: _movie.name,
    }
    console.log(obj);
    dispatch({
      type: MOVIE_UPDATE_STARTED,
      payload: obj,
    });
  };

  return (
    <div>
      <MovieForm
        title='Update a movie'
        buttonTitle='update'
        movie={movie}
        onCancel={() => history.push('/app/movies/list')}
        onSubmit={updateMovie}
      />
    </div>
  );
};

export default MovieUpdate;

Then, the actual problem is that when I use the default prop on the text field the information appears without any problem, but if i use defaultValue it is empty.然后,实际的问题是,当我在文本字段上使用默认道具时,信息显示没有任何问题,但如果我使用defaultValue ,它是空的。

Ok, I kind of got the answer, I read somewhere that the defaultValue can't be used int the rendering.好的,我得到了答案,我在某处读到 defaultValue 不能在渲染中使用。 So I cheat in a way, I set the properties multiline and row={1} (according material-ui documentation) and I was able to edit this field an receive a value to display it in the textfield所以我在某种程度上作弊,我设置了属性 multiline 和 row={1} (根据 material-ui 文档),我能够编辑这个字段并接收一个值以在文本字段中显示它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM