1

I've build the following example to learn about react/redux. Here is the reducer I have :

const tableFilter = (state = 0, action) => {
    if(action.type === 'SET_TABLE_DATA') {
        return state + 1;
    }
        return state;
  }

At that point everything works fine. However I'm trying to follow this tutorial APPLYING REDUX REDUCERS TO ARRAYS and when updating my reducer with the following code :

//========Reducer===================
const INITIAL_STATE = {
  value: 0,
  warning: false
}
const update = (state, mutations) => Object.assign({}, state, mutations)
const tableFilter = (state = INITIAL_STATE, action) => {
  if(action.type === 'SET_TABLE_DATA') {
    return state = update(state, { value: state.value + 1 })
  }
  return state;
}
const FilterApp = combineReducers({tableFilter});
//==================================

const DisplayTable = ({test, DisplayTable}) => {
  return (
    <div>
      <button onClick={DisplayTable}>{test}</button>
      <p></p>
    </div> 
  )
}

function mapStateToProps(state) {
  return {
      test: state.tableFilter
  };
};

const mapDispachToProps = (dispatch) => {
  return {
    DisplayTable: () => {
      dispatch (setTableFilter());
    }
  };
};

const AppTable = connect(mapStateToProps, mapDispachToProps)(DisplayTable);
ReactDOM.render(
  <Provider store={createStore(FilterApp)}>
    <App />
  </Provider>,
  document.getElementById('root')

I've got this error ...Objects are not valid as a React child (found: object with keys {value, warning})....

Looking at others questions here it seems to be a recurrent issue, however the solution is always different. What does this error message really mean ? how can I solve it in my case ? Is there something I need to modify in my component ?

here is my JSBin with the updated reducer.

thanks

2
  • 2
    I don't see a component in the code you've posted. You've posted a Redux reducer. That error is coming from React and is likely due to your render method returning objects that aren't React components. Commented Jul 26, 2016 at 15:47
  • Hey Brandon. Thanks. I've updated my post to make it more clear. tell me if that's ok now ? Commented Jul 26, 2016 at 16:00

1 Answer 1

6

You're trying to render an object directly in JSX and that just isn't allowed.

<button onClick={DisplayTable}>{test}</button>

Here test is an object, not a valid React child. Try

<button onClick={DisplayTable}>{String(test.warning)} {test.value}</button> 

Updated JSBIN

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

3 Comments

@SimonBreton just updated your JSBin and it works just fine there. Where did you even get the setState function?
Ok it's working. Thanks a lot :) So you mean that by modifying my reducer I'm now rendering an object ?
Actually it was only because of the warning: false and that's why you told me to add the string stuff. If I took of this warning thing everything work just fine with {test.value} Thanks.

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.