question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Input cannot use with recompose's withStateHandlers ?

See original GitHub issue

Description

So I’m trying to use react-materialize with recompose’s withStateHandlers to make a controlled input component with a default value. Here is the code (I want the starting text to be ‘hi’):

// BottomModal.js

import React from 'react'
import { Modal, Input, Button } from 'react-materialize'
import { compose, withStateHandlers } from 'recompose'

const enhance = withStateHandlers(
  // States
  {
    name: 'hi',
    price: 0
  },
  // State Handlers
  {
    handleNameChange: (state, props) => event => ({
      name: event.target.value
    }),
    handlePriceChange: (state, props) => event => ({
      price: parseInt(event.target.value)
    }),
    resetInput: (state, props) => () => ({
      name: '',
      price: 0
    })
  }
)

const BottomModal = (props) => {
  console.log(props.name)
  return (
    <Modal
      header={props.header}
      bottomSheet
      trigger={
        <Button style={{position: 'fixed', right: '24px', bottom: '24px'}} floating large waves='light' icon='add' />
      }
      actions={
        <div>
          <Button onClick={props.resetInput} modal='close' flat className='grey lighten-5'>ยกเลิก</Button>
          <Button onClick={props.onConfirm} modal='close' waves='light'>เพิ่ม</Button>
        </div>
      }
    >
      <Input value={props.name} onChange={props.handleNameChange} label='รายการ' />
      {/*
        normal <input /> is working fine btw. But it cannot have label like the <Input /> 
        <input value={props.name} onChange={props.handleNameChange} type='text' />
      */}

    </Modal>
  )
}

export default enhance(BottomModal)

But this gave me a warning:

Warning: Input contains an input of type text with both value and defaultValue props.
Input elements must be either controlled or uncontrolled (specify either the value prop,
or the defaultValue prop, but not both). Decide between using a controlled or uncontrolled
input element and remove one of these props.
More info: https://fb.me/react-controlled-components

And I tried to make name to be an empty string, and the warning is gone:

  // States
  {
    name: '',
    price: 0
  },

~I tried to do this again using traditional React Component (extends React.Component, setState, handleChange, etc.) and it works fine (even with name: 'hi').~ https://github.com/react-materialize/react-materialize/issues/496

So I’m wondering what did I do wrong, or recompose is not fully compatible with react-materialize.

Expected behavior: Work normally like normal <input /> or like without recompose

Actual behavior: throws a warning

Versions

react-materialize: ^2.0.7 materialize-css: ^0.100.2 react: ^16.2.0

Thanks for helping out!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
alextrasterocommented, Mar 6, 2018

Hey @kykungz, have you tried with defaultValue on <Input />? Instead of value

0reactions
alextrasterocommented, Mar 24, 2019

Try version react-materialize@3.x works for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question about error message from withHandlers #403 - GitHub
I'm new to using recompose and am trying to refactor some code using withState() & withHandlers . The project is using next.js so...
Read more >
How to add context to recompose's withHandlers()?
In my example I wrote onChange(input) and added an extra 'level' to the withHandlers.onChange function to accept the extra argument, but ...
Read more >
Simplify your React components with Apollo and Recompose
Let's look how we can use the withState container from Recompose to build a React component that has both a search box and...
Read more >
How to migrate from Recompose to React Hooks - Medium
Unable to reuse stateful logic between components; Each input needs it's own onChange handler and for a large form it could get quite...
Read more >
How to use recompose withState to add state to functional ...
Recompose is a library with utility functions for functional React components. We are going to take a closer look at how we can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found