0

我是反应和反应火库的新手。谁能告诉我如何将路由参数添加到 firestoreconnect 功能。我的代码如下

class Exam extends Component {
  state = {
    id: null,
  };
  componentDidMount() {
    let id = this.props.match.params.examId;
    this.setState({
      id: id,
    });
  }
  render() {
    // console.log(this.props.questions);
    const { auth, questions } = this.props;
    console.log(questions);
    if (!auth.uid) {
      return <Redirect to="/" />;
    }

    return (
      <div className="container" style={{ width: "8 rem" }}>
        <br />
        <br />
        <br />
        <Form>
          {["checkbox", "radio"].map((type) => (
            <div key={`default-${type}`} className="mb-3">
              <Form.Check
                disabled
                type={type}
                label={`disabled ${type}`}
                id={`disabled-default-${type}`}
              />
            </div>
          ))}
          <Button variant="primary" type="submit">
            Submit
          </Button>
        </Form>
        <h1>Exam id is {this.state.id}</h1>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    auth: state.firebase.auth,
    questions: state.firestore.ordered.questions,
  };
};
export default compose(
  connect(mapStateToProps),
  firestoreConnect([
    {
      collection: "questions",
    },
  ])
)(Exam);

我想在 firestoreConnect 中添加一个“doc”参数并将我的“id”分配给“doc”。有任何想法吗???

4

1 回答 1

1

我必须更改 firestoreconnect 功能,如下所示,

firestoreConnect((props) => [
    {
      collection: "questions",
      doc: props.match.params.examId,
    },
  ])

谢谢你们。。

于 2020-09-11T14:29:46.540 回答