Updating parents props via child's state Component










0















I have a Field Component (redux-form) that calls a custom ImageInput component that uploads an image, then spits out the src.



I need to pass that src from my child component, to the parent where it updates the parents state via a handler.



I've tried to follow this answer, but am a bit confused as my setup seems a little different: How to update parent's state in React?



Here is my parent component



handleChange(e) 
console.log(e)
this.setState([e.target.name]: e.target.value);


<Field
...this.props
component=ImageInput
name="templating.img"
onChange=this.handleChange.bind(this)
/>


and then my child where the image is inputted and uploaded



constructor(props) 
console.log('props', props);
super(props);
this.state =
src: null

console.log(this.state)


_imgUpload = (e) =>
e.preventDefault();
// console.log(e.target.files)
if (e.target.files.length === 1)
let file = e.target.files[0]
loadImage(e.target.files[0])
.then(uploadThumbor)
.then(formatImage)
.then((src, dataUri) =>
this.setState(src: src)
console.log('img', src)
)



/* Snippet where the image upload occurs */
<div style=styles.image>
<div style=styles.defaultPreview></div>
<div style=styles.sample></div>
<input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
</div>


After an image has been upload, I am setting the state in the child component. I need to pass that state to the parent where it'll update it's state. I dont think the onChange=this.handleChange.bind(this) is correct for this parent component in this instance (it is correct for other Field components that are simple inputs).



Any help would be awesome.










share|improve this question


























    0















    I have a Field Component (redux-form) that calls a custom ImageInput component that uploads an image, then spits out the src.



    I need to pass that src from my child component, to the parent where it updates the parents state via a handler.



    I've tried to follow this answer, but am a bit confused as my setup seems a little different: How to update parent's state in React?



    Here is my parent component



    handleChange(e) 
    console.log(e)
    this.setState([e.target.name]: e.target.value);


    <Field
    ...this.props
    component=ImageInput
    name="templating.img"
    onChange=this.handleChange.bind(this)
    />


    and then my child where the image is inputted and uploaded



    constructor(props) 
    console.log('props', props);
    super(props);
    this.state =
    src: null

    console.log(this.state)


    _imgUpload = (e) =>
    e.preventDefault();
    // console.log(e.target.files)
    if (e.target.files.length === 1)
    let file = e.target.files[0]
    loadImage(e.target.files[0])
    .then(uploadThumbor)
    .then(formatImage)
    .then((src, dataUri) =>
    this.setState(src: src)
    console.log('img', src)
    )



    /* Snippet where the image upload occurs */
    <div style=styles.image>
    <div style=styles.defaultPreview></div>
    <div style=styles.sample></div>
    <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
    </div>


    After an image has been upload, I am setting the state in the child component. I need to pass that state to the parent where it'll update it's state. I dont think the onChange=this.handleChange.bind(this) is correct for this parent component in this instance (it is correct for other Field components that are simple inputs).



    Any help would be awesome.










    share|improve this question
























      0












      0








      0








      I have a Field Component (redux-form) that calls a custom ImageInput component that uploads an image, then spits out the src.



      I need to pass that src from my child component, to the parent where it updates the parents state via a handler.



      I've tried to follow this answer, but am a bit confused as my setup seems a little different: How to update parent's state in React?



      Here is my parent component



      handleChange(e) 
      console.log(e)
      this.setState([e.target.name]: e.target.value);


      <Field
      ...this.props
      component=ImageInput
      name="templating.img"
      onChange=this.handleChange.bind(this)
      />


      and then my child where the image is inputted and uploaded



      constructor(props) 
      console.log('props', props);
      super(props);
      this.state =
      src: null

      console.log(this.state)


      _imgUpload = (e) =>
      e.preventDefault();
      // console.log(e.target.files)
      if (e.target.files.length === 1)
      let file = e.target.files[0]
      loadImage(e.target.files[0])
      .then(uploadThumbor)
      .then(formatImage)
      .then((src, dataUri) =>
      this.setState(src: src)
      console.log('img', src)
      )



      /* Snippet where the image upload occurs */
      <div style=styles.image>
      <div style=styles.defaultPreview></div>
      <div style=styles.sample></div>
      <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
      </div>


      After an image has been upload, I am setting the state in the child component. I need to pass that state to the parent where it'll update it's state. I dont think the onChange=this.handleChange.bind(this) is correct for this parent component in this instance (it is correct for other Field components that are simple inputs).



      Any help would be awesome.










      share|improve this question














      I have a Field Component (redux-form) that calls a custom ImageInput component that uploads an image, then spits out the src.



      I need to pass that src from my child component, to the parent where it updates the parents state via a handler.



      I've tried to follow this answer, but am a bit confused as my setup seems a little different: How to update parent's state in React?



      Here is my parent component



      handleChange(e) 
      console.log(e)
      this.setState([e.target.name]: e.target.value);


      <Field
      ...this.props
      component=ImageInput
      name="templating.img"
      onChange=this.handleChange.bind(this)
      />


      and then my child where the image is inputted and uploaded



      constructor(props) 
      console.log('props', props);
      super(props);
      this.state =
      src: null

      console.log(this.state)


      _imgUpload = (e) =>
      e.preventDefault();
      // console.log(e.target.files)
      if (e.target.files.length === 1)
      let file = e.target.files[0]
      loadImage(e.target.files[0])
      .then(uploadThumbor)
      .then(formatImage)
      .then((src, dataUri) =>
      this.setState(src: src)
      console.log('img', src)
      )



      /* Snippet where the image upload occurs */
      <div style=styles.image>
      <div style=styles.defaultPreview></div>
      <div style=styles.sample></div>
      <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
      </div>


      After an image has been upload, I am setting the state in the child component. I need to pass that state to the parent where it'll update it's state. I dont think the onChange=this.handleChange.bind(this) is correct for this parent component in this instance (it is correct for other Field components that are simple inputs).



      Any help would be awesome.







      reactjs components state react-props






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 14:32









      Ryan JayRyan Jay

      304




      304






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I get what you are trying to do.
          You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?



          I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.



          If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.



          Parent



          handleChange(e) 
          console.log(e)
          this.setState([e.target.name]: e.target.value);


          handleSrcChange(src)
          this.setState( imageSrc: src );


          <Field
          ...this.props
          component=ImageInput
          name="templating.img"
          onChange=this.handleChange.bind(this)
          onSrcChange=this.handleSrcChange.bind(this)
          />



          Input Component



          _imgUpload = (e) => 
          e.preventDefault();
          // console.log(e.target.files)
          if (e.target.files.length === 1)
          let file = e.target.files[0]
          loadImage(e.target.files[0])
          .then(uploadThumbor)
          .then(formatImage)
          .then((src, dataUri) =>
          this.props.onSrcChange(src);
          console.log('img', src)
          )



          /* Snippet where the image upload occurs */
          <div style=styles.image>
          <div style=styles.defaultPreview></div>
          <div style=styles.sample></div>
          <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
          </div>





          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302608%2fupdating-parents-props-via-childs-state-field-component%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I get what you are trying to do.
            You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?



            I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.



            If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.



            Parent



            handleChange(e) 
            console.log(e)
            this.setState([e.target.name]: e.target.value);


            handleSrcChange(src)
            this.setState( imageSrc: src );


            <Field
            ...this.props
            component=ImageInput
            name="templating.img"
            onChange=this.handleChange.bind(this)
            onSrcChange=this.handleSrcChange.bind(this)
            />



            Input Component



            _imgUpload = (e) => 
            e.preventDefault();
            // console.log(e.target.files)
            if (e.target.files.length === 1)
            let file = e.target.files[0]
            loadImage(e.target.files[0])
            .then(uploadThumbor)
            .then(formatImage)
            .then((src, dataUri) =>
            this.props.onSrcChange(src);
            console.log('img', src)
            )



            /* Snippet where the image upload occurs */
            <div style=styles.image>
            <div style=styles.defaultPreview></div>
            <div style=styles.sample></div>
            <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
            </div>





            share|improve this answer



























              0














              I get what you are trying to do.
              You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?



              I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.



              If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.



              Parent



              handleChange(e) 
              console.log(e)
              this.setState([e.target.name]: e.target.value);


              handleSrcChange(src)
              this.setState( imageSrc: src );


              <Field
              ...this.props
              component=ImageInput
              name="templating.img"
              onChange=this.handleChange.bind(this)
              onSrcChange=this.handleSrcChange.bind(this)
              />



              Input Component



              _imgUpload = (e) => 
              e.preventDefault();
              // console.log(e.target.files)
              if (e.target.files.length === 1)
              let file = e.target.files[0]
              loadImage(e.target.files[0])
              .then(uploadThumbor)
              .then(formatImage)
              .then((src, dataUri) =>
              this.props.onSrcChange(src);
              console.log('img', src)
              )



              /* Snippet where the image upload occurs */
              <div style=styles.image>
              <div style=styles.defaultPreview></div>
              <div style=styles.sample></div>
              <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
              </div>





              share|improve this answer

























                0












                0








                0







                I get what you are trying to do.
                You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?



                I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.



                If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.



                Parent



                handleChange(e) 
                console.log(e)
                this.setState([e.target.name]: e.target.value);


                handleSrcChange(src)
                this.setState( imageSrc: src );


                <Field
                ...this.props
                component=ImageInput
                name="templating.img"
                onChange=this.handleChange.bind(this)
                onSrcChange=this.handleSrcChange.bind(this)
                />



                Input Component



                _imgUpload = (e) => 
                e.preventDefault();
                // console.log(e.target.files)
                if (e.target.files.length === 1)
                let file = e.target.files[0]
                loadImage(e.target.files[0])
                .then(uploadThumbor)
                .then(formatImage)
                .then((src, dataUri) =>
                this.props.onSrcChange(src);
                console.log('img', src)
                )



                /* Snippet where the image upload occurs */
                <div style=styles.image>
                <div style=styles.defaultPreview></div>
                <div style=styles.sample></div>
                <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
                </div>





                share|improve this answer













                I get what you are trying to do.
                You said that you are using redux form. Is this the only input in the form? Why is it uploading the image separately as soon as it changes?



                I ask this cause the call to the api is normally dispatched form the parent container. If this api call is specific to this page I would suggest moving it to the parent and maintain the src state in the parent. If you need it here you can always pass it down as prop.



                If this is not an option you can always have a separate callback function similar to the handleOnChange. It would be called when you get the new src from the backend. This will allow you to maintain the src in the parent state as you wanted. In this case, if you need to have the src in the input component you can do the same as in the other solution, pass it down as a prop.



                Parent



                handleChange(e) 
                console.log(e)
                this.setState([e.target.name]: e.target.value);


                handleSrcChange(src)
                this.setState( imageSrc: src );


                <Field
                ...this.props
                component=ImageInput
                name="templating.img"
                onChange=this.handleChange.bind(this)
                onSrcChange=this.handleSrcChange.bind(this)
                />



                Input Component



                _imgUpload = (e) => 
                e.preventDefault();
                // console.log(e.target.files)
                if (e.target.files.length === 1)
                let file = e.target.files[0]
                loadImage(e.target.files[0])
                .then(uploadThumbor)
                .then(formatImage)
                .then((src, dataUri) =>
                this.props.onSrcChange(src);
                console.log('img', src)
                )



                /* Snippet where the image upload occurs */
                <div style=styles.image>
                <div style=styles.defaultPreview></div>
                <div style=styles.sample></div>
                <input type="file" style=styles.uploadPreview accept="image/*" onChange=this._imgUpload />
                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 15:51









                Agustina ChaerAgustina Chaer

                62




                62



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302608%2fupdating-parents-props-via-childs-state-field-component%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    政党

                    天津地下鉄3号線