React Firestore Problem: Passing url parameter to firestoreconnect

210 views Asked by At

I'm a bit newbie in react and react firestore. Can anyone tell me how to add a route parameter to firestoreconnect funtion. My code is below

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);

I'd like to add a "doc" parameter in the firestoreConnect and asign my "id" to "doc". Any Ideas???

1

There are 1 answers

0
Shafayet Rahat On

I have to change the firestoreconnect function like below,

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

Thanks to you guys..