Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
72 views
in Technique[技术] by (71.8m points)

javascript - How to move modal into its own component from current component

How to make separate component for modal which depends on an API call in current component in React

I have the following API call

class Designs extends Component {
//after constructor I have a function which makes an api call:
async componentDidMount() {
    const designs = await axios.get(`${DESIGN}`);
    this.setState({ rawDesigns: designs.data.documents }); //rawDesigns is an array

And I have the following modal in the render method

render() {
    const {
      designs,
      showModal,
      selectedDesign,
      rawDesigns,
      currentStep,
    } = this.state;

return(
 {showModal && (
          <Modal toggleModal={this.togglePageModal} pageModal={true}>
            <h2 style={{ textAlign: "center", width: "100%" }}>
              {rawDesigns[selectedDesign].name}
            </h2>
            <span
              style={{
                textAlign: "center",
                width: "100%",
                letterSpacing: "3px !important",
                fontWeight: "bold",
                color: "#5252ED",
                marginTop: "5px"
              }}
            >
              {currentStep === 1 && `BASIC DETAILS`}
              {currentStep === 2 && `ARCHITECTURE`}
              {currentStep === 3 && `TEAMS`}
            </span>
            <DotSteps
              totalSteps={3}
              currentStep={currentStep}
              onChange={currentStep => this.setState({ currentStep })}
              className="flex-jfe"
            />
            {currentStep === 1 && (
              <div className="w-100 flex flex-col">
                <ContentCard>
                  <span style={{ color: "#5252ED", fontWeight: "bold" }}>
                    Type
                  </span>
                  <h3 className="mt0">{rawDesigns[selectedDesign].type}</h3>
                </ContentCard>

As you can see this modal uses the state variables as well as info from the api call in the Designs component. Is it possible for me to create a separate component for this modal and then use it in the render method in Designs?

question from:https://stackoverflow.com/questions/65889574/how-to-move-modal-into-its-own-component-from-current-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

React Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.

https://reactjs.org/docs/portals.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...