-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FunctionStringVar generates JavaScript exception when provided via a State attribute #4643
Comments
When the column_defs prop is coming from a state var (reactive mode), then it doesn't seem to be working with functions defined inline. As a workaround, you can set the column_defs imperatively from the state, rather than setting columns. @rx.event
def update_columns(self) -> None:
self.columns = [
ag_grid.column_def(
field="path",
value_formatter=rx.vars.FunctionStringVar(
f"(params) => params.value.join({self.separator!r})" # had to change this line as well
),
),
ag_grid.column_def(field="created"),
ag_grid.column_def(field="modified"),
ag_grid.column_def(
field="size",
value_formatter=rx.cond(
self.human_size,
human_size,
rx.Var.create(None),
),
),
]
return ag_grid.api("ag_grid_format_1").setGridOption("columnDefs", self.columns) When the FunctionStringVar is serialized and passed through the When the I don't know if it makes sense to eval FunctionVar from a state, will have to consider it architecturally, but I don't think a specific fix for this is coming any time soon. Let me know if you have any issues with the workaround. |
This seems resolved. |
For the record here are some issues with the workaround and how they were resolved;
|
This is expected to some degree. React derives its state from the component you give to the component and on re-render, these things will be forgotten. There's an underlying issue though. FunctionVars are not serialized well as props. This is something I should address in my function var PR. In any case, we can add something similar to dynamic component serializer/deserializer such that it knows how to run anonymous functions returned from the backend. |
Describe the bug
When using a FunctionStringVar to provide a function to the value_formatter of an ag_grid column_def via a State attribute, the value_formatter is ignored and there is a JavaScript exception of:
AG Grid: value should be either a string or a function
If the column_def containing the value_formatter of type FunctionStringVar is instead defined directly in the ag_grid configuration, then the exception doesn't occur and the value_formatter works.
To Reproduce
Just run the following code:
test_reflex_ag_grid.zip
There is a button called "Set columns" which sets the state var and causes the columns to be displayed after the ag_grid is built. You'll see the JavaScript exception in the Browser and you'll note the formatting didn't get applied.
Expected behavior
No exception, and the formatting specified by value_formatter should be applied.
Screenshots
![image](https://private-user-images.githubusercontent.com/17627242/403558156-9c635cfd-1132-4acb-a252-905082f19614.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzEzNDcsIm5iZiI6MTczODg3MTA0NywicGF0aCI6Ii8xNzYyNzI0Mi80MDM1NTgxNTYtOWM2MzVjZmQtMTEzMi00YWNiLWEyNTItOTA1MDgyZjE5NjE0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA2VDE5NDQwN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWQyMDUzYjgzMDMxZmY5ZmNmMWY1MDI1YTYyYTEzZjQ5ZGIwZDczMWRlMjRjM2RjNjJjYzBlZGZlNGQ5NTAzZmImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.xKLI3L56s8BthwXD7HCi5U4pcbNLIjgmAsAJ0SvLjc4)
When you first run the code you see:
Then you press the "Set columns" button and you see:
![image](https://private-user-images.githubusercontent.com/17627242/403557526-3d7aa779-ff9f-4c77-b929-831331824b5f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzg4NzEzNDcsIm5iZiI6MTczODg3MTA0NywicGF0aCI6Ii8xNzYyNzI0Mi80MDM1NTc1MjYtM2Q3YWE3NzktZmY5Zi00Yzc3LWI5MjktODMxMzMxODI0YjVmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMDYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjA2VDE5NDQwN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTcwMTJkM2Q1NDY1ZmRkNTYyYjEzMGU2NGE1ZmFlNzAzZTMwODg5MmZjYzE1ZGYwNTdkYWUwNmUwOWQwOGRkMjQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.rsQjaG50SZuiuv13RAiRUa_HyeMObOKyzBGfxzz9Cl8)
Note how the Size column does not have the proper formatting (it should have KB and MB suffix as appropriate) and the JavaScript exception on the right.
Specifics (please complete the following information):
Additional context
The code provided above was adapted from the working example code below of using a FunctionStringVar to provide a function to the value_formatter of an ag_grid column_def that was provided by @masenf
repro_ag_grid_value_formatter.zip
The text was updated successfully, but these errors were encountered: