0

Component method gets called for both Title and Content fields when React is initially rendered. However, while additional input change in Title field calls the renderField method, the Content field doesn't seem to do so. This is reflected in the console log where the initial component load produced 2 "input changed" and for additional changes in the title, "input change" prints, but no responses occur when changes are made to the content field. What's going on?

import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';

class PostNew extends Component {

    renderField(field) {
        console.log('input changed')
        return(
            <div>
                <label>{field.label}</label>
                <input
                    type="text"
                    {...field.input}
                />
            </div>
        );
    }

    render() {
        return(
            <form>
                <Field
                    label="Title"
                    name="title"
                    component={this.renderField}
                />
                <Field
                    label="Content"
                    name="content"
                    component={this.renderField}
                />
            </form>
        )
    }
}


export default reduxForm({
    form: 'PostsNewForm' // form property is the name of the form
})(PostNew);
4
  • I pasted your renderField and render methods into my React project and it worked for both. Not sure why it's not working for you. Commented May 28, 2017 at 0:17
  • Could you try it in this project? github.com/datumsays/React-Redux-Error Commented May 28, 2017 at 0:35
  • It's the pretty much the same code. I'm wondering if this is a package issue. Commented May 28, 2017 at 0:35
  • I'm downloading your project right now Commented May 28, 2017 at 0:37

2 Answers 2

1

Ok I was able to replicate your issue. The problem is that redux-form isn't just plug and play. You need to add it into your root reducer as shown below:

//src/reducers/index.js

import { combineReducers } from 'redux';
import {reducer as form} from 'redux-form';

const rootReducer = combineReducers({
  state: (state = {}) => state,
  form
});

export default rootReducer;

The form fields work as expected for me once I added that. I've uploaded a copy of your Github project with a working example:

https://github.com/hellojebus/so-Answer

Sign up to request clarification or add additional context in comments.

8 Comments

Okay. Let me try your suggestion
Tried it, but it didn't work. One thing I want to note though is that in either cases I do get an error saying the following: "bundle.js:1212 Uncaught Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's render method, or you have multiple copies of React loaded "
Though I've been ignoring it since I was still able to make method call when changing the title input.
A javascript error will cause your code to stop executing, so we will need to handle that issue before moving forward. Make sure you are not referencing React twice in any of your files.
Also, are you using refs in your code at all? I don't see any in the github link you posted.
|
0

For anyone who encountered a similar issue as mine. The source of the problem ultimately came from the fact that the redux-form package didn't install at all. So either use yarn install redux-form or try npm but ensure check that npm works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.